Given an integer, write a function to determine if it is a power of two.
public class Solution {
public boolean isPowerOfTwo(int n) {
if(n<0)
return false;
if(Integer.bitCount(n)==1)//n的二进制出现1的位数==1的话,就是2的次方
return true;
else
return false;
}
}
转载请注明原文地址: https://ju.6miu.com/read-21745.html