LeetCode #231 Easy

Power of Two

Determine whether an integer is a power of two using a bitwise trick, not repeated division.

mathbit-manipulationrecursion
Open on LeetCode ↗
02

Intuition

The trap is forgetting that zero and negative numbers are not powers of two, and letting the classic bit trick silently accept zero anyway. n & (n-1) == 0 is the standard test: a power of two has exactly one set bit, and subtracting 1 flips that bit off and turns every bit below it on, so ANDing the two clears everything to zero. The problem is that 0 & -1 also equals 0 in two's complement, so the bit trick alone says 'yes' for zero even though zero is not a power of two. The fix is to guard n > 0 before ever touching the bit trick -- once positivity is confirmed, the AND trick is airtight because a positive integer's binary representation has no ambiguity about its highest bit.

How to spot this pattern

n & (n - 1) clears the lowest set bit. A power of two has exactly one set bit, so clearing it leaves zero — one operation, no loop, no division. The same identity underpins counting set bits (Brian Kernighan's algorithm) and is worth recognising on sight.

03

Approach

1

Guard non-positive values first

If n <= 0, return False immediately. This handles zero and all negative numbers before the bit trick has a chance to misfire on them.

2

Apply the single-set-bit test

For n > 0, compute n & (n - 1). A power of two in binary looks like a single 1 followed by zeros; subtracting 1 turns that pattern into all 1s below the original bit and clears the original bit itself, so the AND of the two is 0 exactly when n has one set bit.

3

Return the comparison

Return whether n & (n - 1) equals 0. Combined with the earlier positivity guard, this is a full, O(1), constant-space test with no loops or divisions.

04

Solution & live demo

1class Solution:
2 def isPowerOfTwo(self, n: int) -> bool:
3 positive = n > 0
4 if not positive:
5 return False
6 single_bit = (n & (n - 1)) == 0
7 return single_bit
05

Common pitfalls

Forgetting the positivity guard

✗ Wrong
return (n & (n - 1)) == 0
✓ Right
if n <= 0: return False
return (n & (n - 1)) == 0

0 & -1 is 0, so zero reports as a power of two. Negative numbers can also slip through in two's-complement representations. Powers of two are strictly positive, and the guard must come first.

Looping with repeated division

✗ Wrong
while n % 2 == 0: n //= 2
return n == 1
✓ Right
return (n & (n - 1)) == 0

Correct but O(log n) with a division each round. The bit trick answers in a single operation — and demonstrating that you know it is the point of the question.

Using a floating-point logarithm

✗ Wrong
return math.log2(n).is_integer()
✓ Right
return (n & (n - 1)) == 0

Floating point rounding makes large values like 2^29 occasionally test as non-integral, or a near-miss test as exact. Integer bit operations have no such failure mode.

06

Edge cases

n = 0

guarded out by n > 0 check, returns False

negative n

guarded out by n > 0 check, returns False

n = 1 (2^0)

n & (n-1) = 1 & 0 = 0, returns True

large power of two like 2^30

bit trick works identically regardless of magnitude, no overflow in Python

07

Complexity

Time
O(1)
Space
O(1)
single bitwise operation, no loop needed