In a recent project, it involved performing an "or" operation to 2 64 bit bit-strings, but I faced a problem, if the strings were sufficiently large, any bitwise operation would return 0, for example: (this following example may not be a 64 bit bit-string but is sufficiently long to produce the same results).
console.log("10101001010100101010101001010000001010100101010010"|"000100101010100101001010101001010101010100101010010100");
// returns 0
Is there a custom method I could write for time efficient bitwise operations in Javascript, or a way I could represent these bit-strings differently? If you need any clarifications regarding the question, feel free to ask.
Since the introduction of BigInt in EcmaScript, you can do the following:
let a = BigInt("0b10101001010100101010101001010000001010100101010010");
let b = BigInt("0b000100101010100101001010101001010101010100101010010100");
console.log((a|b).toString(2));
This is what you would do when the input comes in some text format. If however, you just want to have it hard coded in JavaScript, you can also use the literal notation (with suffix n
):
let a = 0b10101001010100101010101001010000001010100101010010n;
let b = 0b000100101010100101001010101001010101010100101010010100n;
console.log((a|b).toString(2));
Note that the call of .toString(2)
is only there to show the binary representation; the expression a|b
really is a number of type bigint
.