Last active
April 9, 2019 00:01
-
-
Save reqshark/ed18be16cf96fdcc9aa4a73b6176a786 to your computer and use it in GitHub Desktop.
binary math conversion
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
// take a number 6 -> binary | |
console.log(convert(10)) | |
function convert(n){ | |
let remainder = [] | |
while (n){ | |
remainder.push(n % 2) | |
n = parseInt(n/2) // keep dividing it | |
} | |
let l = remainder.length | |
let ret = '' | |
while (l--) | |
ret +=remainder[l] | |
return ret | |
// or just do remainder.reverse().join('') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment