Power of Four

easy

Check if a number is a power of four

Power of Four

Key Insight

Must be power of 2 AND have 1-bit at even position. Use mask 0x55555555.

Step 1Powers of 4
16
16
=
0
0
0
1
0
0
0
0
76543210
Pos 4 (even)

1, 4, 16, 64... 1-bit at positions 0, 2, 4, 6...

1 / 5

Learn the Pattern

Practice the Code

Step-by-Step Walkthrough: Power of Four

Must be power of 2 AND have 1-bit at even position. Use mask 0x55555555.

  1. Powers of 4

    1, 4, 16, 64... 1-bit at positions 0, 2, 4, 6...

  2. Check Power of 2

    First: n & (n-1) == 0?

  3. Even Position Mask

    0x55555555 = 01010101... (even positions)

  4. Check Position

    16 & mask = 16 ≠ 0. Bit is at even position!

  5. Result

    16 IS a power of 4!