Skip to content

Instantly share code, notes, and snippets.

@ICeZer0
ICeZer0 / TwosComplement.js
Created January 17, 2019 14:04
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;
});