Power of Two - Bit Trick

A power of two has exactly one set bit. Using Brian Kernighan's trick: if n & (n - 1) == 0, then nn has at most one set bit.

But we also need n>0n > 0 (since 00 has no set bits but isn't a power of two).

The check is: n > 0 && (n & (n - 1)) == 0.