MCPcopy Create free account
hub / github.com/careercup/CtCI-6th-Edition-JavaScript / countBits

Function countBits

chapter05/5.6 - Conversion/conversion.js:1–6  ·  view source on GitHub ↗
(number)

Source from the content-addressed store, hash-verified

1var countBits = function(number) {
2 var string = number.toString(2);
3 return string.split('').reduce((numBits, char) => {
4 return char === '1' ? numBits + 1 : numBits;
5 }, 0);
6};
7
8var conversion = function(number1, number2) {
9 return countBits(number1 ^ number2);

Callers 1

conversionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected