Get elementary values from a flagged enum
The way to test for a power of two, where x is an unsigned integer type is

( x != 0 ) &amp;&amp; ( ( x &amp; ( x - 1 ) ) == 0 )


To understand why this works, it's fairly easy to see that if x is a power of two, then x &amp; (x -1 ) is zero.

If x is not a power of two, then only the bits up to the first non-zero bit are changed when you subtract one, and that is not the most significant bit, so the most significant bit is not cleared, and so x &amp; (x - 1 ) is non-zero, as required.

Zero is a special case - it's not a power of two, so an extra test is needed, also ( x - 1 ) overflows if x is zero.