Given an integer, write a function to determine if it is a power of two.
===================================================================
题目链接:https://leetcode.com/problems/power-of-two/
题目大意:判断一个数是否为2的幂。
思路:位操作。
参考代码:
class Solution {
public:
bool isPowerOfTwo(int n) {
if ( n == 0 )
return false ;
while ( n )
{
if ( n & 1 && ( n >> 1 ) != 0 )
return false ;
n >>= 1 ;
}
return true ;
}
};