231.2的幂
https://leetcode-cn.com/problems/power-of-two/
//这种方法是关于幂的通解 暴力模拟
class Solution {
public:
bool isPowerOfTwo(int n) {
while(n && n %2 ==0)
{
n /=2;
}
return n == 1;
}
};
/*
位运算解法:
判断奇偶
(x & 1) == 1 ---等价---> (x % 2 == 1)
(x & 1) == 0 ---等价---> (x % 2 == 0)
x / 2 ---等价---> x >> 1
x &= (x - 1) ------> 把x最低位的二进制1给去掉
x & -x -----> 得到最低位的1
x & ~x -----> 0
class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && ( n & (n-1) ) == 0;
}
};
*/