Skip to content

Instantly share code, notes, and snippets.

@reqshark
Last active April 9, 2019 00:01
Show Gist options
  • Save reqshark/ed18be16cf96fdcc9aa4a73b6176a786 to your computer and use it in GitHub Desktop.
Save reqshark/ed18be16cf96fdcc9aa4a73b6176a786 to your computer and use it in GitHub Desktop.
binary math conversion
// 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