Power Of Two
EasyBit Manipulation · NVIDIA
Given an integer n, return True if n is a power of two, meaning n == 2^x for some integer x >= 0, and False otherwise.
Note that 1 is a power of two (2^0), while 0 and all negative numbers are not.
Follow-up: can you do it without loops or recursion?
Example 1
- Input:
- n = 64
- Output:
- True
- Explanation
- 64 is 26. In binary it is
1000000, a single 1 bit, which is exactly what every power of two looks like.
Example 2
- Input:
- n = 96
- Output:
- False
- Explanation
- 96 is
1100000in binary. It has two 1 bits, so it is not a power of two (it is 64 + 32).
Example 3
- Input:
- n = -16
- Output:
- False
- Explanation
- No power of two is negative, so any
n <= 0returnsFalse.
Constraints
-2^31 <= n <= 2^31 - 1