Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Write a method that returns true if an int n is a positive power of two, without loops, and explain why it works.

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.

Java
// n = 8 // n = 12
// n = 0000 1000 // n = 0000 1100
// n - 1 = 0000 0111 // n - 1 = 0000 1011
// n & (n - 1) = 0000 0000 -> 0 // n & (n - 1) = 0000 1000 -> 8, not zero
n & (n - 1) for a few values
nn in binaryn - 1 in binaryn & (n - 1)Power of two?
8100001110yes
12110010111000 (8)no
1000100000yes
000001111 ... 1111 (-1)0no, needs n > 0
MIN_VALUE1000 ... 00000111 ... 11110no, needs n > 0
The last two rows pass the AND test but are not positive powers of two, which is why we also check n > 0.

3Writing the Method

Now we can write the method. It is one expression, with no loop and no division.

Java
static boolean isPowerOfTwo(int n) {
return n > 0 && (n & (n - 1)) == 0;
}
isPowerOfTwo(1); // true 2 to the power 0
isPowerOfTwo(64); // true
isPowerOfTwo(96); // false 1100000 has two bits set
isPowerOfTwo(0); // false rejected by n > 0
isPowerOfTwo(Integer.MIN_VALUE); // false rejected by n > 0
isPowerOfTwo(-8); // false

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.

Java
static boolean isPowerOfTwo(int n) {
return n > 0 && Integer.bitCount(n) == 1;
}
Integer.bitCount(64); // 1
Integer.bitCount(Integer.MIN_VALUE); // 1, so the n > 0 check is still needed
Integer.highestOneBit(100); // 64, the power of two at or below 100

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.

What to say in the interviewAbout 80 seconds aloud

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.
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/