题目内容
Given an integer, write a function to determine if it is a power of two.
题目分析
判断一个数是不是2的冥。
只要对该数减一,然后&操作就可以了。
例如 8=1000,8-1=0111,1000&0111=0;即可知道该数是不是2的冥了。
public class Solution {
public boolean isPowerOfTwo(int n) {
if(n<1) return false;
return (0==((n-1)&n));
}
}