Power of Four
Power of Four is LeetCode 342 (Easy). Given an integer n, return true if n = 4^x for some integer x ≥ 0, and false otherwise.
ncan be zero or negative, and neither is ever4^x.nfits in a 32-bit signed int, so the largest value that must pass is4^15 = 2^30.- Follow-up: answer without loops or recursion.
- -2³¹ <= n <= 2³¹ - 1
Intuition
Every power of four is also a power of two, because 4^x = 2^(2x). So a power of four has exactly one set bit, and that bit sits at an even position (bit 0, 2, 4, …, 30).
- One set bit:
n & (n - 1) == 0checks this, the same test used in Power of Two. - Even position: the mask
0x55555555is0101…0101in binary, a 1 at every even position. ANDing the lone bit with it gives a non-zero result only when the bit is at an even position.
Powers of two that are not powers of four (2, 8, 32, …) put their bit at an odd position, so the mask removes it.
Approach
Reject n ≤ 0
A power of four is always positive, so return false for zero and negative numbers first. This also keeps 0 from slipping through the bit test, since 0 & -1 is 0.
Check for exactly one set bit
Compute n & (n - 1). Subtracting 1 clears the lowest set bit, so the result is 0 only when that bit was the only one. If it is not 0, n is not even a power of two.
Check that the bit is at an even position
AND n with 0x55555555. The mask keeps bits 0, 2, 4, …, 30 and drops the odd ones. A non-zero result means the single bit landed on an even position, so n = 2^(2x) = 4^x.
Code in Python, C++ and Java
10000.n & (n - 1) clears the lowest set bit and leaves 0, so bit 4 is the only one: 16 = 2^4. Now the solid pads matter. They sit at the even positions, which is where every 4^x puts its bit, because 4^x = 2^(2x).0x55555555 has a 1 at position 4, so the AND keeps the bit and is not 0. Since 4 = 2 × 2, 16 = 4^2.1000.n & (n - 1) clears the lowest set bit and leaves 0, so bit 3 is the only one: 8 = 2^3. Now the solid pads matter. They sit at the even positions, which is where every 4^x puts its bit, because 4^x = 2^(2x).101.n & (n - 1) leaves 4, not 0, so 5 is not even a power of two. Note that both bits sit at even positions: the mask alone would have said true. That is why this check comes before the mask.1.n & (n - 1) clears the lowest set bit and leaves 0, so bit 0 is the only one: 1 = 2^0. Now the solid pads matter. They sit at the even positions, which is where every 4^x puts its bit, because 4^x = 2^(2x).0x55555555 has a 1 at position 0, so the AND keeps the bit and is not 0. Since 0 = 2 × 0, 1 = 4^0.Common pitfalls
Using only the mask
return n > 0 and (n & 0x55555555) != 0
return n > 0 and (n & (n - 1)) == 0 and (n & 0x55555555) != 0
n = 5 is 101: both bits are at even positions, so the mask alone says true. The one-bit test is what rules out numbers like 5, 17 and 21.
Mixing up the two masks
return n > 0 and (n & (n - 1)) == 0 and (n & 0xAAAAAAAA) != 0
return n > 0 and (n & (n - 1)) == 0 and (n & 0x55555555) != 0
0xAAAAAAAA has its 1s at the odd positions, so it keeps 2, 8 and 32 and drops 1, 4 and 16. With != 0 every answer flips: n = 16 returns false. That mask only works paired with == 0.
Edge cases
1 = 4⁰. Its one bit is at position 0, which is even, so the mask keeps it and the answer is true with no special case.
The largest power of four in a signed 32-bit int (4¹⁵). Bit 30 is even and the mask covers it, so it passes.
Complexity
n. Dividing by 4 in a loop would take up to 15 steps.Power of Four FAQ
Is every power of 2 a power of 4?
No. Every power of 4 is a power of 2, but only the even powers of 2 are powers of 4: 1, 4, 16, 64 pass, while 2, 8, 32, 128 do not. That is exactly the gap the even-position mask closes.
Why does (n - 1) % 3 == 0 also work?
Once n is known to be a power of two, 4^x - 1 is always divisible by 3, because 4 ≡ 1 (mod 3). An odd power 2 · 4^x leaves remainder 2 mod 3, so 2 · 4^x - 1 leaves remainder 1. So n > 0, n & (n - 1) == 0 and (n - 1) % 3 == 0 together are another loop-free test.
What does 0x55555555 look like in binary?
Hex digit 5 is 0101, so eight of them give 0101 0101 … 0101: 32 bits with a 1 at positions 0, 2, 4, …, 30. Its partner 0xAAAAAAAA (hex A = 1010) has the 1s at the odd positions instead.