342. power of four

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
Example: Given num = 16, return true. Given num = 5, return false.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class  {
public:
bool isPowerOfFour(int num) {
if(num==1)return true;
if(num<0)return false;
if(num<=2)return false;
int count=0;int location=0;
for(int i=0;i<32;i++){
count+=(num>>i)&1;
location=location==0?(((num>>i)&1)==1?i:0):location;
}
if(count!=1)return false;
if(location%2)return false;
return true;
}
};