Power of Two solution explained
An integer n is a power of two exactly when n > 0 and n & (n - 1) == 0. A power of two has a single 1 bit, and subtracting 1 turns that bit off and every bit below it on, so the AND of the two is zero; any other positive number keeps its highest 1 bit. The check is O(1). The n > 0 test is required, because 0 and negative numbers would otherwise slip through or behave differently across languages.
By Evin JohnPublished 7 min read
The problem in plain words
You are given an integer, usually one that fits in a signed 32-bit type, and must say whether it can be written as 2 raised to a whole-number power. The powers are 1 (2^0), 2, 4, 8, 16 and so on. Zero is not a power of two, and no negative number is. Interviewers often add "can you do it without a loop?", which points to the bit trick, but a correct loop is a fine starting point.
| n | Binary | Power of two? |
|---|---|---|
| 1 | 1 | Yes, 2^0 |
| 64 | 1000000 | Yes, 2^6 |
| 96 | 1100000 | No, two 1 bits (64 + 32) |
| 0 | 0 | No |
| -8 | negative | No, even though 8 is |
| 1073741824 | 1 followed by 30 zeros | Yes, 2^30, the largest in signed 32-bit range |
| 2147483647 | 31 ones | No |
What the question is testing
Power of Two checks whether you can connect a numeric property to a binary picture. Once you say "a power of two has exactly one bit set", the rest follows. Interviewers listen for the explanation of why n & (n - 1) clears the lowest set bit, for the guard against zero and negatives, and for awareness of how your language treats integers. It is a short question, so the quality of the reasoning carries most of the weight. The same bit fact underlies Number of 1 Bits, where it counts set bits one at a time; here you only need to know whether there is more than one.
Follow-ups
- Power of four. It must be a power of two, and its single bit must sit in an even position: n & 0x55555555 is non-zero in 32-bit terms. Alternatively, a power of two whose remainder by 3 is 1.
- Power of three. No bit trick exists. In 32-bit range, 3^19 = 1162261467 is the largest power of three, so n > 0 and 1162261467 % n == 0 works because its only divisors are powers of three.
- Round up to the next power of two. Subtract 1 first (so an exact power stays where it is), smear the highest bit downward with shifts and ORs, then add 1; in Python, 1 << (n - 1).bit_length() does the same for n >= 1.
- Which power is it? In Python, n.bit_length() - 1; in JavaScript, 31 - Math.clz32(n) for 32-bit values.
First version: keep halving
Reject anything below 1. Then, while the number is even, divide it by 2. A power of two reaches exactly 1; any other number gets stuck at an odd value greater than 1, which is the odd factor it carried all along. The loop runs once per trailing zero bit, at most about 31 times for 32-bit input, so it is O(log n) and perfectly acceptable as a first answer.
@@power-of-two/slow.py@@Expected version: one bit operation
Look at what subtracting 1 does in binary. For 64, which is 1000000, n - 1 is 0111111: the single 1 bit became 0 and every bit below it became 1. The two numbers share no 1 bits, so their AND is 0. For 96, which is 1100000, n - 1 is 1011111, and the top bit survives the AND, giving 1000000, which is not 0. In general n & (n - 1) removes the lowest 1 bit, so the result is 0 only when that was the only 1 bit. A second formula uses two's complement: n & -n isolates the lowest set bit, and it equals n only if no other bit is set.
@@power-of-two/fast.py@@JavaScript needs a word of caution. Its bitwise operators convert their operands to 32-bit integers first. For integer input in the signed 32-bit range the one-liner is right, but on bigger values it can be wrong: 2^32 + 2 reports true, because only its low 32 bits, 2, are seen. Non-integers are also truncated, so 2.5 reports true. For safe integers beyond 32 bits, the halving loop with plain arithmetic is exact, which is why the code below keeps both and labels their domains.
@@power-of-two/fast.js@@Cost and alternatives
| Approach | Time | Correct for | Note |
|---|---|---|---|
| Halve while even | O(log n) | Any integer size | Simple first answer |
| n > 0 and n & (n - 1) == 0 | O(1) | Any size in Python; 32-bit integers in JavaScript | The expected answer |
| n > 0 and n & -n == n | O(1) | Same as above | Uses two's complement |
| n > 0 and 2^30 % n == 0 | O(1) | Positive signed 32-bit values | 2^30 is the largest power of two in range; checked on 200,000 values |
| Floating-point log2 is a whole number | O(1) | Small values only | Reports true for 2^53 + 1 in Python and 2^53 + 2 in JavaScript |
Edge cases
- 0: n & (n - 1) is 0 & -1, which is 0, so without the n > 0 guard zero would count as a power of two.
- Negative numbers: the most negative 32-bit value, -2^31, also has a single 1 bit in two's complement; the guard rejects it.
- 1, which is 2^0 and must return true.
- 2^30, the largest power of two in signed 32-bit range, and 2^31 - 1, which is all ones.
- In JavaScript, values outside the 32-bit range and non-integers, as described above.
Common slips
- Leaving out n > 0.
- Writing n & n - 1 == 0 without parentheses. In C, C++, Java and JavaScript, == binds tighter than &, so it means n & ((n - 1) == 0). Python happens to parse it as intended, but write the parentheses anyway.
- Using Math.log2 or math.log2 and checking for a whole number. It looks clever and fails on large inputs.
- Counting all the set bits with a loop and comparing with 1. It is correct but does more work than needed; mention that it is the general version of the same idea.
Explaining it in the room
Fictional example
- Candidate
- Powers of two in binary are a single one followed by zeros. So the question is whether exactly one bit is set.
- Candidate
- Subtracting one flips that bit off and turns on everything below it, so n AND n minus one is zero for a power of two. For anything else the highest bit survives.
- Candidate
- Zero would pass that check by accident, so I'll require n greater than zero first. In JavaScript I'd note that bitwise ops are 32-bit, which matches the usual input range.
Related problems
Fibonacci Number is another short numeric question where the representation of numbers comes up. Count Primes is a divisibility problem solved by a structural trick instead of testing each number. Number of 1 Bits and Counting Bits use the same lowest-set-bit operation to count.
Natively for practice
One-line answers still need a clear spoken reason, and bit tricks are easy to state badly. In a practice round on Mac or Windows, Natively can read the question from a screenshot, check that your explanation of n & (n - 1) is right, and suggest the power-of-four or power-of-three variant next. The transcript afterwards shows whether you mentioned the zero case without being asked.
Questions people ask
How do you check if a number is a power of two?
Return n > 0 and (n & (n - 1)) == 0. A power of two has one set bit, and subtracting 1 clears it and sets all lower bits, so the AND is zero only for powers of two.
Why do you need n > 0 in the power of two check?
Zero passes the bit test, because 0 & -1 is 0, and in two's complement the most negative integer also has a single set bit. Neither is a power of two.
Can you check a power of two without bit operations?
Yes: divide by 2 while the number is even and check whether you end at 1. It takes O(log n) steps. Avoid floating-point log2, which gives wrong answers on large inputs.
Does n & (n - 1) work in JavaScript?
For integers in the signed 32-bit range, yes. JavaScript converts bitwise operands to 32 bits, so larger values or fractions can give wrong results; use a halving loop for those.
Where to go next
Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.
Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.
