if((i & (i-1)) == 0) { i 是2的幂次方}
public class Test {
public static void main(String args[]) {
isPowerOfTwo(2);
isPowerOfTwo(4);
isPowerOfTwo(6);
isPowerOfTwo(9);
isPowerOfTwo(1024);
isPowerOfTwo(2044);
}
private static void isPowerOfTwo(int num) {
System.out.println();
System.out.println("num is: " + num);
if (((num - 1) & num) == 0) {
System.out.println("The number is a power of two");
} else {
System.out.println("The number is a NOT A power of two");
}
}
}
http://stackoverflow.com/questions/5082314/power-of-2-formula-help