Better HN
top
new
best
ask
show
jobs
Log in
Sign up
Better HN
Comment
by
msluyter
· 16y ago · 0 comments
Correct me if I'm wrong, but I don't believe the bit twiddling trick would work on floating point representations, would it?
Comments
[–]
effigies
16y ago
With 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.
[–]
mfukar
16y ago
Correct, it would not. (I'm assuming IEEE floats)