Power of Four

Easy
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Power of Four

Approach

First check if n is a power of two using n & (n - 1) === 0. Then verify the single set bit is at an even position (bit 0, 2, 4, ...) by ANDing with the mask 0x55555555 which has 1s at all even bit positions.

Complexity Analysis

Time
O(1)
Space
O(1)

Pattern

Bit Masking

Why It Works

Powers of four are powers of two where the set bit falls at an even index (4^0=bit 0, 4^1=bit 2, 4^2=bit 4). The 0x55555555 mask isolates exactly those positions.

Updated Feb 2026