![](https://secure.gravatar.com/avatar/7ae1f973711b916a188ec3962f4aa701.jpg?s=120&d=mm&r=g)
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