Skip to content

Instantly share code, notes, and snippets.

@mscdex
Created March 13, 2016 20:07
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mscdex/2381ff505778a987cc37 to your computer and use it in GitHub Desktop.
Save mscdex/2381ff505778a987cc37 to your computer and use it in GitHub Desktop.
Fastest bit counting for 32-bit numbers in javascript
function bitCount(u) {
// https://blogs.msdn.microsoft.com/jeuge/2005/06/08/bit-fiddling-3/
const uCount = u - ((u >> 1) & 0o33333333333) - ((u >> 2) & 0o11111111111);
return ((uCount + (uCount >> 3)) & 0o30707070707) % 63;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment