3 ms·
Detecting a power of two is easy with just (x != 0 && (x & x-1) == 0). A portable way of determining how many bits are in an object is sizeof(object) * CHAR_BI
by mikeash 10y ago
Detecting a power of two is easy with just (x != 0 && (x & x-1) == 0).
A portable way of determining how many bits are in an object is sizeof(object) * CHAR_BIT. No reason to use the 8 version.
- _RPM 10y agoThere are most likely many ways to check if a number is a power of two. I'm not sure if my solution is more efficient. It seems your expression would create more code than mine.