231. Power of Two
easyAsked at IntelGiven an integer n, return true if it is a power of two. Intel interviewers ask because the one-line bit trick (n > 0 && (n & (n-1)) === 0) tests whether you've internalized the binary structure of powers of two — a property that shows up everywhere in cache-line alignment, ring buffers, and address arithmetic.
By Sam K., Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Intel loops.
- Glassdoor (2026-Q1)— Intel SWE-new-grad phone-screen reports list 'is power of two' as a 5-minute warm-up.
- GeeksforGeeks (2025-07)— Intel Interview Experience archives include this in 'must-know bit tricks' for hardware-SWE rounds.
- LeetCode discuss (2025-10)— Intel-tagged company-frequency list pairs this with reverse-bits and Hamming weight.
Problem
Given an integer n, return true if it is a power of two. Otherwise, return false. An integer n is a power of two, if there exists an integer x such that n == 2^x.
Constraints
-2^31 <= n <= 2^31 - 1
Examples
Example 1
n = 1trueExplanation: 2^0 = 1.
Example 2
n = 16trueExplanation: 2^4 = 16.
Example 3
n = 3falseExample 4
n = -16falseExplanation: Negative numbers are never powers of two by the problem's definition (x must produce a positive 2^x).
Approaches
1. Repeated division (brute force)
While n is even and greater than 1, divide by 2. If you end at exactly 1, it was a power of two.
- Time
- O(log n)
- Space
- O(1)
function isPowerOfTwoBrute(n) {
if (n <= 0) return false;
while (n > 1) {
if (n % 2 !== 0) return false;
n = Math.floor(n / 2);
}
return true;
}Tradeoff: Works on any base by swapping the divisor. Logarithmic time. Easy to read but not the answer Intel wants.
2. Bit trick (optimal)
A positive power of two has exactly one set bit. n & (n - 1) clears the lowest set bit; if that result is 0, n had exactly one set bit.
- Time
- O(1)
- Space
- O(1)
function isPowerOfTwo(n) {
return n > 0 && (n & (n - 1)) === 0;
}Tradeoff: Single expression, branch-free after the n > 0 guard, no loop. This is the canonical answer — anything else on a power-of-two question signals lack of bit-arithmetic fluency.
Intel-specific tips
Intel cares about this because power-of-two checks are everywhere in systems code — buffer sizes for ring buffers (so % becomes & (mask)), DMA alignment, cache-line indexing, FFT sizes. Mention 'this is the same trick that lets ring buffers use bitwise-AND instead of modulo' or 'this is why ALIGN-UP-to-power-of-two is one op in firmware'. That context elevates the answer from 'I know a trick' to 'I know why the trick matters'.
Common mistakes
- Forgetting the n > 0 guard. For n = 0, the expression 0 & (-1) is 0, so the trick incorrectly returns true.
- Including negative powers of two by accident — the problem definition restricts x to non-negative; -16 is not a power of two even though |-16| is.
- Using `Math.log2(n) % 1 === 0` — floating-point comparison; fails for large n due to precision (2^30 returns weird results in some engines).
Follow-up questions
An interviewer at Intel may pivot to one of these next:
- Power of Three (LC 326) — no bit trick; use the largest power of 3 in int range and check divisibility.
- Power of Four (LC 342) — power of two AND set bit is at an even position: `(n & 0xaaaaaaaa) === 0`.
- Given n, return the next power of two >= n. (Hint: round up via OR-shift chain or use clz.)
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does (n & (n - 1)) === 0 test for exactly one set bit?
If n has exactly one set bit at position k, then n - 1 has zeros at position k and ones at all lower positions. ANDing those two together produces zero. If n has more than one set bit, subtracting 1 only clears the lowest set bit and below; ANDing keeps the higher set bits intact, so the result is nonzero.
Why does Intel ask such a 'simple' question?
It's a 30-second filter: if you fumble it, you don't get to the harder rounds. The Intel hardware-SWE rubric explicitly wants candidates who reach for bit tricks reflexively because that's the day-to-day vocabulary in firmware and driver work.
Free learning resources
Curated free links for this problem.