Skip to content

Instantly share code, notes, and snippets.

@Risyandi
Created March 4, 2020 13:54
Show Gist options
  • Save Risyandi/2b7fbdcbf9d234656980730cdebd7361 to your computer and use it in GitHub Desktop.
Save Risyandi/2b7fbdcbf9d234656980730cdebd7361 to your computer and use it in GitHub Desktop.
/**
* created by : risyandi @2020
*/
function countBits(num, bitNum) {
let binary = Number(num).toString(2);
let temp = "";
let result = null;
for (let index = 0; index < binary.length; index++) {
let binNum = binary[index];
if (binNum == bitNum) {
temp += binNum;
} else if (binNum > 2) {
return null;
}
}
result = parseInt(temp.length);
console.log(result);
return result;
}
countBits(13, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment