Created
May 10, 2022 01:52
-
-
Save scolton99/172257e10222354e0d8d2d438c9dcb1e to your computer and use it in GitHub Desktop.
Counting bits in a 32-bit number.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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