Checking if a Number is a Power of Two A neat way to check if a number is a power of 2 using XOR is:
boolean isPowerOfTwo(int n) { return (n > 0) && ((n & (n - 1)) == 0); } This works because powers of two have only one bit set in their binary representation.