Math Fundamentals18 sections · 814 units
Open in Course

Problem - Power of Two

LeetCode 231

Given an integer nn, return true if it is a power of two. Otherwise, return false. An integer nn is a power of two if there exists an integer xx such that n=2xn = 2^x.

Examples: 1=201 = 2^0, 2=212 = 2^1, 4=224 = 2^2, 16=2416 = 2^4. These are all powers of two. But 33, 55, 66 are not.

This problem uses boolean logic with bitwise operations. Before reading on, think: what's special about the binary representation of powers of two?