Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Return `n > 0 && (n & (n - 1)) == 0`. The positive check rules out 0 and negatives.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you know the n & (n-1) bit trick and guard the edge cases.
Where candidates slip
Missing the n > 0 guard, letting 0 pass.
How to stand out
Explain why n & (n-1) clears the lowest set bit; it also powers bit-counting.
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
Clear-lowest-bit trick
1Hint 1Explore hintHide hint
What's special about a power of two in binary?
2Hint 2Explore hintHide hint
What does `n & (n - 1)` do to the lowest set bit?
3Hint 3Explore hintHide hint
Which non-powers slip through without an `n > 0` guard?
A · Core idea
A power of two has exactly one set bit (1, 10, 100, 1000 in binary). The trick n & (n - 1) == 0 (with n > 0) tests precisely that.
Why `n & (n-1)` clears the lowest set bit. Subtracting 1 flips the lowest set bit to 0 and turns all bits below it to 1 (e.g. 1000 − 1 = 0111). ANDing the two leaves every bit unset if and only if there was a single set bit. So for a power of two the result is 0; for anything with ≥2 set bits it's non-zero.
Why the `n > 0` guard. 0 has no set bits and would pass n & (n-1) == 0 falsely; negative numbers (two's complement) have many set bits but also need excluding. The positivity check rules both out.
Why bits beat a divide loop. Repeatedly dividing by 2 works but is O(log n) and misses the elegant single-operation insight that interviewers probe.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = 16, then n = 616 = 10000
16 & 15 = 10000 & 01111
0 → power of two16 → true, 6 → falseC · Code with teaching comments
D · Why it works and complexityO(1) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
n & (n-1) clearing the lowest set bit is one of the most reused bit tricks — also counts bits and detects powers of two.
ComplexityO(1) time, O(1) space — a single subtraction and AND — constant time.
E · Edge cases and pitfalls5n = 1 — 2⁰ = 1, one set bit → true.Explore edge casesHide edge cases
Edge cases handled
n = 12⁰ = 1, one set bit → true.n = 0Guarded out by n > 0 → false.n = -8Negative → false.
Pitfalls
- Forgetting `n > 0`, so 0 wrongly returns true.
- Using a loop dividing by 2 (works but misses the bit insight).
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic boolean isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
1Missing the n > 0 guard
0 & (0-1) == 0 is true, so without the guard zero is wrongly reported as a power of two.
Explore the code comparisonHide the code comparison
Missing the n > 0 guard
0 & (0-1) == 0 is true, so without the guard zero is wrongly reported as a power of two.
Don't write this
return (n & (n - 1)) == 0; // 0 -> true (WRONG)Write this instead
return n > 0 && (n & (n - 1)) == 0;Revise
Carry the pattern into the interview
Pattern to remember
n & (n-1) clears lowest bit
Code pattern to memorize
n > 0 && (n & (n-1)) == 0Rules to remember
- 1Power of two = one set bit.
- 2n & (n-1) == 0.
- 3Require n > 0.
Use this pattern when
- Single-bit / power-of-two checks
Choose another pattern when
- Forgetting to exclude 0 and negatives
Interview takeaway
n & (n-1) == 0 (and n>0) tests for a power of two.
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: n & (n-1) clears lowest bit. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Count Set BitsSame n & (n-1) trick, counting iterations.
- Power of TwoComing soonLeetCode version.