Xit0

Random Scribblings of a Linux Enthusiast

Check if a Number Is a Power of 2 the Easier Way

This one-liner does the trick.

1
((x > 1) && !(x & (x - 1)))

Clue: “every power of 2 has only one bit set in the binary representation”

Comments