Powers of two in binary have exactly one bit set. For example: , , , . All other numbers have multiple bits set or zero bits.
If is a power of two, then flips all bits after the single set bit. For , . Notice .
The boolean condition: n > 0 && (n & (n - 1)) == 0. The first part handles negative numbers and zero. The second checks the bit pattern. Both must be true.