Skip to content
BytePatterns

Power of Four Check

EasyBit Manipulation#bitmask#power-of-two~15m

Problem

Given a whole number n that fits in a signed 32-bit integer, return True if n equals 4 raised to some whole power (1, 4, 16, 64 and so on) and False otherwise. Zero and negative numbers are never powers of four. Try to answer without a loop, using only bit operations.

Examples

Input:  n = 16
Output: True
Why:    16 is 4 squared
Input:  n = 8
Output: False
Why:    8 is a power of two, but its single set bit is in the wrong place
Input:  n = 1
Output: True
Why:    edge case, 4 to the power 0 is 1

Hints

0 / 3

Stuck on the idea rather than the code? Masks and Power of Two covers it.