Last active
December 16, 2015 02:39
-
-
Save gwa/5364271 to your computer and use it in GitHub Desktop.
Get the number of bits set in a binary number.
This file contains 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
function getNumBitsSet(binary) { | |
var numbits = 0; | |
for (var v=binary; v; v>>=1) { | |
numbits += v&1; | |
} | |
return numbits; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment