Hacker News
new
|
past
|
comments
|
ask
|
show
|
jobs
|
submit
login
ChristianMarks
on July 26, 2011
|
parent
|
context
|
favorite
| on:
Ten Ways to Check if an Integer Is a Power Of Two ...
The decrement test for a power of two can be modified to count bits and runs in log n time.
int bits(unsigned n) { int i = 0; while (n > 0) { n &= n-1; i++ } return i; }
axylone
on July 26, 2011
[–]
Much better ways to count bits set:
http://graphics.stanford.edu/~seander/bithacks.html#CountBit...
Guidelines
|
FAQ
|
Lists
|
API
|
Security
|
Legal
|
Apply to YC
|
Contact
Search: