Testing for powers of two
Oct. 29, 2004
4:12 p.m.
There was a discussion some time ago about testing for powers of two. The other day I saw a nifty version go by on the LKML, courtesy of Linus. Namely: is_power_of_two = x && !(x & (x - 1)) which works for all numbers except possibly the number x8000, which if twos complement will probably give an underflow on subtraction of 1. So for signed numbers: is_power_of_two = x > 0 && !(x & (x - 1)) Chuck
7388
Age (days ago)
7388
Last active (days ago)
0 comments
1 participants
participants (1)
-
Charles Harris