Created
August 21, 2017 08:26
-
-
Save xexi/8cc5c8d30e50813fa7c469bca6c543d9 to your computer and use it in GitHub Desktop.
js dec2bin playground
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
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