Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9174933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:50:00+00:00 2026-06-17T16:50:00+00:00

I have a program which I found online which basically tells whether the String

  • 0

I have a program which I found online which basically tells whether the String contains all unique characters, below is the code

private static boolean areCharsUnique(String str) {
        if (str.length() > 256)
            return false;
        int checker = 0;
        for (int i = 0; i < str.length(); i++) {
            int val = str.charAt(i) - 'a';
            if ((checker & (1 << val)) > 0) {
                return false;
            }
            checker |= (1 << val);
        }
        return true;
    }

I am baffled by this line of code if ((checker & (1 << val)) > 0) and also

checker |= (1 << val);

I know that << is a left shift operator, but how exactly left shifting helps in the above situation?

In short how does the above program work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:50:01+00:00Added an answer on June 17, 2026 at 4:50 pm

    This code works under the assumption that ASCII character set has consecutive character in its mapping so that a == 97, b == 98, and so on.

    Starting from this you can calculate a delta distance from the a character, eg 'e' - 'a' = 5. This distance is used to set (through checker |= (1 << val)) a bit in a integer number (which has 32 bits).

    So if a 'e' character is found then bit at index 5 is set to 1 for checker.

    This is done for every char by ensuring that you never find a bit that has been already set previously (through if (checker & (1 << val)) > 0)).

    This works only for lowercase characters (even because int has 32 bits). An HashSet<Character> would be surely better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found some code online for red black trees, and am trying to
I have designed a program which, basically, cuts a geometrical shape into many small
Basically, I have to write a basic program that solves the n-queen problem, which
I have program in which i want to do some graphic algorithms. I found
I have a program which creates JButtons which are then added to a JPanel
I have a program which dynamically generates a GUI. I don't know how many
I have a program which outputs a pair of words surrounded by spaces on
I have a program which prints a multi-page document. The first page is pre-printed
I have a program which I would like to run every X min the
I have a program which runs an external, command line utility and reads the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.