Re:Write A Function To Determine If A Number Is A Power Of 2
by msluyter on Monday October 18, 2010 @12:11PM
(#1804012)
Correct me if I'm wrong, but I don't believe the bit twiddling trick would work on floating point representations, would it?
[ Parent
| on HN ]
Re:Write A Function To Determine If A Number Is A Power Of 2
by effigies on Monday October 18, 2010 @12:18PM
(#1804045)
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.