Given an integer, write a function to determine if it is a power of two.
题目意思:判断某个数是否是2的幂。
方法:直接进行bit运算,判断是否这个数二进制位里有且仅有一个1。
class Solution {
public:
bool isPowerOfTwo(int n) {
int c=0;
while(n!=0)
{
c+=1&n;
if(c>1)
return false;
n>>=1;
}
if(c==1)
return true;
else
return false;
}
};