Power of Two
Easy · bit manipulation
A number is a power of two if it equals 2 multiplied by itself some whole number of times, including zero times. So 1, 2, 4, 8, 16, ... are powers of two, and 0, negative numbers and numbers such as 6 are not.
Given an integer n, return true if n is a power of two and false otherwise.
Examples
Example 1
Input: n = 16
Output: True
16 = 2 * 2 * 2 * 2.
Example 2
Input: n = 12
Output: False
12 lies between 8 and 16, and is not equal to either.
Example 3
Input: n = -8
Output: False
Powers of two are positive, so a negative number never qualifies.
Constraints
- -2^31 <= n <= 2^31 - 1