Answer 5If the number is in the variable n, this C expression will be nonzero if there is just one bit set in the number.
n ? !(n & (n-1)) : 0
Note that it's necessary to explicitly test for zero
Contributor:David Tweed
Published July 2003
E-mail eq@circuitcellar.com with questions or comments.
Back to Questions