LESSON 45 / 75·2 min readIntermediate

Bit manipulation in Java

Good to knowConcept lesson
01 Understand02 Recall03 Prepare

Common operations

  • &, |, ^, ~, <<, >>
  • check even/odd
  • count set bits
  • single number
  • power of two

Example: count set bits

java
int countSetBits(int n) {
    int count = 0;
    while (n > 0) {
        n &= (n - 1);
        count++;
    }
    return count;
}

PAUSE & RECALL

Can you explain it without the notes?

Bit manipulation in Java

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

Common operations

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Important interview questions for Java

Adapted from 15_DSA_JAVA.md · “12. Bit manipulation in Java”. Examples are study fragments; framework behavior can depend on your version.