Skip to content

Instantly share code, notes, and snippets.

@iburlakov
Created February 10, 2020 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iburlakov/0a832705a06a4fdd76e3daec85a9fdcc to your computer and use it in GitHub Desktop.
Save iburlakov/0a832705a06a4fdd76e3daec85a9fdcc to your computer and use it in GitHub Desktop.
// calculate the number of "ones" on tan integer
function calculate(num) {
let count = 0;
do {
count += num & 1;
num = num >> 1;
} while (num);
return count;
}
function test(num) {
console.log(`${num} has ${calculate(num)} ones.`)
}
test(0);
test(1);
test(2);
test(3);
test(8);
test(0xf);
test(0xff);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment