Hacker News
new
|
past
|
comments
|
ask
|
show
|
jobs
|
submit
login
sgk284
on July 1, 2009
|
parent
|
context
|
favorite
| on:
Low Level Bit Hacks You Absolutely Must Know
The best solution I know of is:
a, i = 11, 0 while a > 0: a, i = a & a-1, i + 1
If there are k bits set out of n bits, it only does k iterations.
ralph
on July 3, 2009
[–]
That's
http://graphics.stanford.edu/~seander/bithacks.html#CountBit...
Consider applying for YC's Summer 2026 batch! Applications are open till May 4
Guidelines
|
FAQ
|
Lists
|
API
|
Security
|
Legal
|
Apply to YC
|
Contact
Search: