Given an integer, write a function to determine if it is a powero two.
Solution
如果一个整数是2的幂,那么它的二进制形式最高位为1,其余各位为0
等价于:n & (n - 1) = 0,且n > 0
- Java
1 2 3 4 5 | |
- Python
1 2 3 4 5 6 7 8 | |
Given an integer, write a function to determine if it is a powero two.
如果一个整数是2的幂,那么它的二进制形式最高位为1,其余各位为0
等价于:n & (n - 1) = 0,且n > 0
1 2 3 4 5 | |
1 2 3 4 5 6 7 8 | |