Bit Manipulation Bit basics
105. Power of Two
Easy
Given an integer n, return true if n is a power of two, and false otherwise.
A power of two is a number 2k with k a whole number, 0 or more: 1, 2, 4, 8, 16 and so on. 1 counts, because it is 20.
Example 1
Input: n = 64
Output:
trueExplanation: 64 = 26.
Example 2
Input: n = 96
Output:
falseExplanation: 96 = 25 × 3. It is even, but the factor 3 means it is not a power of two.
Example 3
Input: n = 0
Output:
falseExplanation: Every power of two is at least 1, so 0 and the negative numbers are never powers of two.
Check your understanding
Work out the answer for this input before you write any code.
n = 1
Constraints
-253 ≤ n ≤ 253
Clarifying questions
What you would ask an interviewer before writing any code.