46 ms·
One they missed: (x & -x) == x
by mansr 15y ago
One they missed: (x & -x) == x
- Jabbles 15y agoUnfortunately this doesn't work for x=0.
- rcfox 15y ago2^-infinity == 0
- mansr 15y agoSo make it x && blah. Do I really have to state the obvious?
- Jabbles 15y agoYou're a computer programmer (probably). Yes you have to state the obvious...
- chime 15y agoThat was #9: int isPowerOfTwo (unsigned int x) { return ((x != 0) && !(x & (x - 1))); }
- quacker 15y agoThis is method 10 in the article, since -x is equivalent to ~x+1.
- cube13 15y agoThis works for negative values of x, though. It should be: ((x>0) && ((x & -x) == x))