In your code, you are parsing the string to binary numbers. parseInt("0001100", 2)
is 0b001100
, or 12
in decimal. The numbers you're attempting to parse are way larger than MAX_SAFE_INTEGER
, but that's kind of irrelevant anyway...
It's not possible to add leading zeroes to a number using xor, since the leading numbers are implicitly there anyway, so it won't change the representation of the number:
(0b0000 ^ 0b100) === 0b100
, so toString(2)
-ing that will leave you with "100"
.
You'll need to go through your string character by character and build a string from it.
let x = "11000000000000000000000000000000000000000000000000000000";
let mask = "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
let result = mask.split("").reduceRight(function(partialResult, value, index) {
if (index < x.length && x.charAt(x.length - index - 1) === "1") {
return partialResult + "1";
} else {
return partialResult + "0";
}
}, "");
console.log("Result is: " + result);