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;
}
};
Given an integer, write a function to determine if it is a power of two.
若n&(n-1)的值为0,则n为2的整数次方。
class Solution {
public:
bool isPowerOfTwo(int n) {
if (n <= 0) return false;
return (n & (n-1)) == 0;
}
};