Power of Two
Desicription
Given an integer, write a function to determine if it is a power of two.
Example 1:
代码语言:javascript复制Input: 1
Output: true
Explanation: 20 = 1
Example 2:
代码语言:javascript复制Input: 16
Output: true
Explanation: 24 = 16
Example 3:
代码语言:javascript复制Input: 218
Output: false
Solution
代码语言:javascript复制class Solution {
public:
bool isPowerOfTwo(int n) {
if(n <= 0) {
return false;
} else {
return !(n&(n-1));
}
}
};