question: Given an integer, write a function to determine if it is a power of two.
思路:power of 2表明只有最高位为1,其余为均为0,则若n&(n-1)为0,返回true,否则返回false.
class Solution {
public:
bool isPowerOfTwo(int n) {
if(n <= 0)
return false;
return !(n&(n-1));
}
};