231. power of two

Given an integer, write a function to determine if it is a power of two.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class  {
public:
bool isPowerOfTwo(int n) {
if(n==1)return true;
if(n&1||n<=0)return false;
int count=0;
while(n){
count+=n&1;
n>>=1;
}
if(count==1)return true;
return false;
}
};