Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Loop: while n != 0, do `n &= (n-1)` and increment a counter. Runs once per set bit — O(set bits).
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you know Kernighan's clear-lowest-bit loop.
Where candidates slip
A fixed 32-iteration scan, or forgetting to update n.
How to stand out
Explain why iterations equal set bits; mention Integer.bitCount as the library option.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Brian Kernighan's algorithm
1Hint 1Explore hintHide hint
What does `n & (n-1)` remove each time?
2Hint 2Explore hintHide hint
Count how many times you can clear a bit before n is 0.
3Hint 3Explore hintHide hint
Why is this better than checking all 32 bit positions?
A · Core idea
Count the 1-bits (Hamming weight) with Brian Kernighan's algorithm: repeatedly do n &= (n - 1), which clears the lowest set bit, and count how many times until n is 0.
Why the iteration count equals the bit count. Each n &= (n-1) removes exactly one set bit (the lowest), so the loop runs once per 1-bit. For a sparse number this is far fewer iterations than checking all 32 bits.
Why it beats the naive 32-iteration scan. A loop that tests each of the 32 bit positions always does 32 iterations; Kernighan's does only as many as there are set bits — e.g. 1 iteration for a power of two.
Built-ins exist (Integer.bitCount, bin(n).count('1')), but the Kernighan loop is the algorithm interviewers want to see and explains the clear-lowest-bit idiom.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = 7 (111)111
& 110 = 110
count=13C · Code with teaching comments
D · Why it works and complexityO(number of set bits) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
Built-ins exist (Integer.bitCount / bin(n).count('1')) but the Kernighan loop is the algorithm interviewers want to see.
ComplexityO(number of set bits) time, O(1) space — one iteration per set bit (≤ 32), each O(1).
E · Edge cases and pitfalls5n = 8 (1000) — One set bit → 1.Explore edge casesHide edge cases
Edge cases handled
n = 8 (1000)One set bit → 1.n = 0No set bits → 0.n = 2^31-1All low 31 bits set → 31.
Pitfalls
- Looping a fixed 32 times unnecessarily.
- Infinite loop if you forget to update n.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int countBits(int n) { int count = 0; while (n != 0) { n &= (n - 1); count++;Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Looping a fixed 32 times unnecessarily.
- Infinite loop if you forget to update n.
Revise
Carry the pattern into the interview
Pattern to remember
Kernighan bit-clear loop
Code pattern to memorize
count=0; while n: n &= n-1; count++Rules to remember
- 1n &= n-1 clears a bit.
- 2Count iterations.
- 3Stops at 0.
Use this pattern when
- Hamming weight
- Bit-set counting
Interview takeaway
Each n &= n-1 removes one set bit — count the steps.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Kernighan bit-clear loop. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Number of 1 BitsComing soonThe LeetCode Hamming-weight version.
- Power of TwoSet-bit count of exactly 1.