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 ;
}
};