Created
February 10, 2020 19:18
-
-
Save iburlakov/0a832705a06a4fdd76e3daec85a9fdcc to your computer and use it in GitHub Desktop.
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
// 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