Created
January 17, 2019 14:04
-
-
Save ICeZer0/e5dac53d5ff6f2a5917ae16e6f42851c to your computer and use it in GitHub Desktop.
Takes a Binary string and will return its signed decimal value
This file contains hidden or 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 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