Skip to content

Instantly share code, notes, and snippets.

@scolton99
Created May 10, 2022 01:52
Show Gist options
  • Save scolton99/172257e10222354e0d8d2d438c9dcb1e to your computer and use it in GitHub Desktop.
Save scolton99/172257e10222354e0d8d2d438c9dcb1e to your computer and use it in GitHub Desktop.
Counting bits in a 32-bit number.
const countBits = num => {
let bits = 0;
for (let i = 0; i < 32; ++i)
bits += !!(num & (1 << i)) ? 1 : 0;
return bits;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment