3 ms·
Pop count returns the number of set bits. So it most useful for figuring out if a number is a power of two. If there are more than 1 "set" bits, then it is NOT
by _RPM 10y ago
Pop count returns the number of set bits. So it most useful for figuring out if a number is a power of two. If there are more than 1 "set" bits, then it is NOT a power of two. However, a non portable way of determining how many bits are in a object would be this expression, assuming 8 bits are in a byte. `sizeof(object) * 8`
- mikeash 10y agoDetecting 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.