题目:
Given an integer, write a function to determine if it is a power of two.
Tags Math Bit Manipulation
Similar Problems (E) Number of 1 Bits
分析:
无
代码:
class Solution {
public:
bool isPowerOfTwo(int n) {
if(n==0)
return false;
if(n==1)
return true;
//处理n>1的情况
int q;
do{
q=n%2;
if(q)
break;
n=n/2;
}while(n>1);
if(n==1)
return true;
else
return false;
}
};