Skip to content

Instantly share code, notes, and snippets.

@ICeZer0
Created January 17, 2019 14:04
Show Gist options
  • Save ICeZer0/e5dac53d5ff6f2a5917ae16e6f42851c to your computer and use it in GitHub Desktop.
Save ICeZer0/e5dac53d5ff6f2a5917ae16e6f42851c to your computer and use it in GitHub Desktop.
Takes a Binary string and will return its signed decimal value
function binaryTwosComplementToInt(binaryString) {
if (binaryString[0] != 1) {
return parseInt(binaryString, 2);
}
let twosComplementResult = "";
binaryString.match(/.{1,1}/g).forEach(str => {
twosComplementResult += (str == 1) ? str = 0 : str = 1;
});
return -parseInt(twosComplementResult, 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment