isPowerOfTwo

时间:2023-03-09 22:33:39
isPowerOfTwo
//Given an integer, write a function to determine if it is a power of two.
public class isPowerOfTwo {
public static boolean isPowerOfTwo(int n) {
if (n == 1)
return true;
else if (n < 0)
return false;
else {
String str = Integer.toBinaryString(n);
for(int i=1;i<str.length();i++)
{
if('1'==str.charAt(i))
return false;
}
}
return true;
} public static void main(String[] args) {
System.out.println(isPowerOfTwo(1024)); }
}