3 ms·
Correct me if I'm wrong, but I don't believe the bit twiddling trick would work on floating point representations, would it?
by msluyter 16y ago
Correct me if I'm wrong, but I don't believe the bit twiddling trick would work on floating point representations, would it?
- mfukar 16y agoCorrect, it would not. (I'm assuming IEEE floats)
- effigies 16y agoWith floating point, the first 9 bits are sign and exponent. You could thus check with: int ispoweroftwo(float x) { return !(*(int*)(&x) << 9); } This only works for exact powers of two, though. It's possible to get something that's really close that will fail this.