Skip to content

Instantly share code, notes, and snippets.

@xexi
Created August 21, 2017 08:26
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 xexi/8cc5c8d30e50813fa7c469bca6c543d9 to your computer and use it in GitHub Desktop.
Save xexi/8cc5c8d30e50813fa7c469bca6c543d9 to your computer and use it in GitHub Desktop.
js dec2bin playground
function dec2bin(dec){
return (dec >>> 0).toString(2); // >>> zero fill right shift due to signed bit
}
function bin2exclamation(bin){
var arr = [];
for(var i=0; i< bin.length; i++){
arr.push(parseInt(bin.substr(i, 1)) === 1 ? 0 : 1);
}
return arr.join('');
}
function bin2int(bin){
return parseInt(bin, 2);
}
var init = 100;
init = dec2bin(init);
console.log(init);
init = bin2exclamation(init);
console.log(init);
init = bin2int(init);
console.log(init);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment