Bit Counting

by Igor Cuckovic

JavaScript

var countBits = function(n) {
    return n ? n.toString(2).match(/1/g).length : 0;
};

console.log(countBits(1234)); // is 10011010010, so the function should return 5