231. Power of Two
2017-10-26 本文已影响0人
YellowLayne
1.描述
Given an integer, write a function to determine if it is a power of two.
2.分析
若n&(n-1)的值为0,则n为2的整数次方。
3.代码
class Solution {
public:
bool isPowerOfTwo(int n) {
if (n <= 0) return false;
return (n & (n-1)) == 0;
}
};