题目描述:
Given an integer, write a function to determine if it is a power of two.
解题思路:
判断方法主要依据2的N次幂的特点:仅有首位为1,其余各位都为0.
代码如下:
public class Solution {
public boolean isPowerOfTwo(int n) {
return (n > 0) && ( n & (n - 1)) == 0;
}
}
2021-09-21 09:06:10
题目描述:
Given an integer, write a function to determine if it is a power of two.
解题思路:
判断方法主要依据2的N次幂的特点:仅有首位为1,其余各位都为0.
代码如下:
public class Solution {
public boolean isPowerOfTwo(int n) {
return (n > 0) && ( n & (n - 1)) == 0;
}
}