Quick revision
The short answer
We can write it as return n > 0 && (n & (n - 1)) == 0. A positive power of two has exactly one bit set, and subtracting one clears that bit and sets every bit below it, so the AND of n and n - 1 is zero only for powers of two. The n > 0 check is needed to reject 0 and Integer.MIN_VALUE, and Integer.bitCount(n) == 1 with n > 0 is an equally clear alternative.
What to remember
In this answer, we saw that a positive power of two has exactly one bit set, and that n - 1 clears that bit and sets all the bits below it, so n & (n - 1) is zero exactly for powers of two. We added the n > 0 check to reject 0 and Integer.MIN_VALUE, noticed the parentheses that the operator precedence requires, and saw that Integer.bitCount(n) == 1 with n > 0 is an equally clear alternative.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We can write it as return n > 0 && (n & (n - 1)) == 0. A positive power of two has exactly one bit set, and subtracting one clears that bit and sets every bit below it, so the AND of n and n - 1 is zero only for powers of two. The n > 0 check is needed to reject 0 and Integer.MIN_VALUE, and Integer.bitCount(n) == 1 with n > 0 is an equally clear alternative.
Why this question is asked
Checking for a power of two comes up more often than it seems. Hash tables round their capacity up to a power of two, memory allocators and buffer pools align sizes to powers of two, and many binary formats require it. Interviewers like this question because the one-line answer is easy to memorise and easy to get slightly wrong: most people forget the edge cases, and many cannot explain why the trick works. In this answer, we will recap how powers of two look in binary, see what n - 1 does to them, write the method, test it on the tricky values, and compare it with Integer.bitCount.
1A Quick Recap: Powers of Two in Binary
In binary, each place is worth twice the place to its right, just as each decimal place is worth ten times the next. So a power of two is a 1 followed only by zeros: 1 is 1, 2 is 10, 4 is 100, 8 is 1000, and 64 is 1000000. Every positive power of two has exactly one bit set, and every number with exactly one bit set is a power of two.
So the question becomes: how do we check that exactly one bit is set, without a loop that looks at all 32 bits? The answer is a small arithmetic trick with n - 1.
2What n - 1 Does to the Bits
When we subtract one from a binary number, we borrow from the lowest 1 bit. That bit becomes 0, and every 0 bit below it becomes 1. The bits above it stay the same. For a power of two, there are no bits above the single 1, so n and n - 1 have no bit in common, and their AND is zero.
For any other positive number there is at least one more 1 bit higher up. Subtracting one only changes the lowest 1 and the zeros below it, so that higher bit is still set in both n and n - 1, and the AND is not zero. That is the whole trick: n & (n - 1) removes the lowest set bit, and the result is zero exactly when there was only one bit to remove.
| n | n in binary | n - 1 in binary | n & (n - 1) | Power of two? |
|---|---|---|---|---|
| 8 | 1000 | 0111 | 0 | yes |
| 12 | 1100 | 1011 | 1000 (8) | no |
| 1 | 0001 | 0000 | 0 | yes |
| 0 | 0000 | 1111 ... 1111 (-1) | 0 | no, needs n > 0 |
MIN_VALUE | 1000 ... 0000 | 0111 ... 1111 | 0 | no, needs n > 0 |
3Writing the Method
Now we can write the method. It is one expression, with no loop and no division.
Notice the parentheses around n & (n - 1). In Java, == binds more tightly than &, so n & (n - 1) == 0 without them would mean n & ((n - 1) == 0), which mixes an int with a boolean and does not compile. The compiler protects us here, but in languages where it compiles silently this is a classic bug.
4Why the n > 0 Check Matters
The two values that fool the trick are 0 and Integer.MIN_VALUE. For 0, n - 1 is -1, which is all ones, but 0 AND anything is still 0, so the test says yes. For Integer.MIN_VALUE, the pattern is a 1 in the sign bit followed by 31 zeros, so it really has exactly one bit set. Subtracting one overflows to Integer.MAX_VALUE, which is a 0 followed by 31 ones, and the AND is 0 again. But MIN_VALUE is -2147483648, a negative number, not a power of two.
The check n > 0 rejects both of them, and every other negative number too. Notice that we put it first. The && operator short-circuits, so for zero or negative input the AND is never computed, although here that is about clarity rather than speed.
5The Alternative: Integer.bitCount
Java also gives us a method that counts set bits directly. Integer.bitCount(n) returns the number of 1 bits, so a positive number with a bit count of one is a power of two. On modern processors the JIT compiler turns bitCount into a single population-count instruction, so it is just as fast.
Which one we choose is a matter of reading. The bitCount version says what it means in words; the AND version is the idiom many developers recognise at a glance. For long values, the same code works with long arithmetic or Long.bitCount. Related methods like Integer.highestOneBit and Integer.numberOfLeadingZeros help when we need to round up to the next power of two, which is what HashMap does with its capacity.
6Follow-up questions
How would you round a number up to the next power of two?
For n greater than 1 we can compute 1 << (32 - Integer.numberOfLeadingZeros(n - 1)). For n = 100, n - 1 is 99, which has 25 leading zeros, so we shift 1 by 7 and get 128. For n = 64 we get 64 itself. We still need to guard against values above 2 to the power 30, where the result would overflow.
Does the trick work for long values?
Yes. n > 0 && (n & (n - 1)) == 0 works the same way for a long, with the literal written as 1L where needed. Long.bitCount(n) == 1 with n > 0 is the alternative. The edge case moves to Long.MIN_VALUE, which the n > 0 check again rejects.
What else is n & (n - 1) useful for?
It clears the lowest set bit, so a loop that repeats n = n & (n - 1) until n is zero runs once per set bit, which is Kernighan's way of counting bits. Its sibling n & -n isolates the lowest set bit, which is how Fenwick trees navigate.
7Conclusion
In this answer, we saw that a positive power of two has exactly one bit set, and that n - 1 clears that bit and sets all the bits below it, so n & (n - 1) is zero exactly for powers of two. We added the n > 0 check to reject 0 and Integer.MIN_VALUE, noticed the parentheses that the operator precedence requires, and saw that Integer.bitCount(n) == 1 with n > 0 is an equally clear alternative.
Open with
I would write return n > 0 && (n & (n - 1)) == 0. The reason it works is that a positive power of two has exactly one bit set in binary, a single one followed by zeros.
Then, if they want more
When I subtract one, that bit is borrowed from, so it becomes zero and every bit below it becomes one. That means n and n minus one share no bits, and their AND is zero. For any other positive number there is a higher one bit that subtraction does not touch, so the AND is not zero.
The n > 0 check is essential for two values. Zero gives zero for the AND, so it would pass, and Integer.MIN_VALUE also has exactly one bit set, the sign bit, and minus one overflows to MAX_VALUE, so it would pass as well even though it is negative.
I also keep the parentheses around the AND because == binds tighter than the ampersand. An equally clear alternative is n > 0 && Integer.bitCount(n) == 1, which the JIT compiles to a single popcount instruction on modern hardware, so I would pick whichever reads better to the team.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write a method that returns true if an int n is a positive power of two, without loops...": https://scrivio-interview-library.netlify.app/java/operators-and-control-flow/07671d6ed5/