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

Examples computed with the code on this page
nBinaryPower of two?
11Yes, 2^0
641000000Yes, 2^6
961100000No, two 1 bits (64 + 32)
00No
-8negativeNo, even though 8 is
10737418241 followed by 30 zerosYes, 2^30, the largest in signed 32-bit range
214748364731 onesNo
Examples computed with the code on this page. Scroll sideways on a small screen.

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@@
Python: divide out factors of 2. O(log n) time, O(1) memory.

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@@
Python: two constant-time bit checks. Python integers have no fixed width, so both work for any size.

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@@
JavaScript: the 32-bit bit check, and a loop that is exact for any safe integer.

Cost and alternatives

For 32-bit input every option is effectively constant; the notes are what matters
ApproachTimeCorrect forNote
Halve while evenO(log n)Any integer sizeSimple first answer
n > 0 and n & (n - 1) == 0O(1)Any size in Python; 32-bit integers in JavaScriptThe expected answer
n > 0 and n & -n == nO(1)Same as aboveUses two's complement
n > 0 and 2^30 % n == 0O(1)Positive signed 32-bit values2^30 is the largest power of two in range; checked on 200,000 values
Floating-point log2 is a whole numberO(1)Small values onlyReports true for 2^53 + 1 in Python and 2^53 + 2 in JavaScript
For 32-bit input every option is effectively constant; the notes are what matters. Scroll sideways on a small screen.

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

Starting from the binary picture
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.

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.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP