Given an integer, write a function to determine if it is a power of two.
2的幂
n&n-1,消掉最低位的1看是否为0
class Solution {
public:
bool isPowerOfTwo(int n) {
return ((n>0) && (n&(n-1))==0);
}
};
Given an integer, write a function to determine if it is a power of two.
2的幂
n&n-1,消掉最低位的1看是否为0
class Solution {
public:
bool isPowerOfTwo(int n) {
return ((n>0) && (n&(n-1))==0);
}
};