easyBit ManipulationMathArrays & Hashing

Power of Four

easyTime: O(1)Space: O(1)

Signals to notice

check if number is a power of 4single bit set AND in even positionbit pattern check

Brute force first

keep dividing by 4 until you either land on 1 or break clean divisibility. That direct path works, but it never reveals the bit pattern that makes the check feel almost immediate.

The key insight

first confirm the number is a power of two, then confirm that its single set bit sits in an even position. The mask 0x55555555 marks exactly those even positions, so the pattern of the bits tells you whether the number truly belongs. Once you hold onto the right piece of information from moment to moment, the problem feels less like trial and error and more like following a shape that was there all along.

What must stay true

Powers of 4 are powers of 2 where the single set bit is at an even position (bit 0, 2, 4,..). The mask 0x55555555 has 1s only at even positions, so AND-ing filters for this. When you keep that truth intact, each local choice supports the larger solution instead of fighting it.

Easy way to go wrong

Not understanding why power of 2 isn't enough — 8 (1000) is a power of 2 but its set bit is at position 3 (odd), so it's not a power of 4. The fix is usually to return to the meaning of each move, not just the steps themselves.

Arrays & Hashing Pattern