Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.





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: