Created
February 1, 2016 17:15
-
-
Save giannispan/4612f6aeceebba496f4a to your computer and use it in GitHub Desktop.
Bit Counting
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
var countBits = function(n) { | |
var bRep = (n >>> 0).toString(2); | |
var digits = (bRep).toString(10).split("").map(function(t){return parseInt(t)}); | |
console.log(digits); | |
var k =0; | |
for (var i = 0; i < digits.length; i++) { | |
if (digits[i] == 1) { | |
k++ ; | |
} | |
} | |
return k; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment