Given an integer, write a function to determine if it is a power of two.
public class Solution231 {
public boolean isPowerOfTwo(int n) {
if(n==1) return true;
if(n%2 == 1) return false;
int cnt = 2;
while (cnt <= Integer.MAX_VALUE) {
if((cnt ^ n) == 0) {
return true;
}
if(cnt > n || cnt <= 0) return false;
cnt *= 2;
}
return false;
}
public static void main(String[] args) {
Solution231 ans = new Solution231();
System.out.println(ans.isPowerOfTwo(6));
}
}