0

Code like this:

let a = 3232237057
let b = 4294967040
let c = a & b
console.log(a.toString(2))
console.log(b.toString(2))
console.log(c.toString(2))

Output is:

11000000101010000000011000000001
11111111111111111111111100000000
-111111010101111111101000000000

I have expected output like:

11000000101010000000011000000001
11111111111111111111111100000000
11000000101010000000011000000000

What is wrong?

momi
  • 332
  • 1
  • 5
  • 17
  • 1
    JS bitwise operations *only* work on 32-bit signed integers. If a number is larger than 32-bit (`2 ^ 31 - 1` or `2147483647`) the bitwise representation is truncated. – VLAZ Nov 19 '21 at 21:01
  • The easiest way to do it is with BigInt - change `a = BigInt(3232237057)` and `b = BigInt(4294967040)` then `c` would be `3232237056n` or `11000000101010000000011000000000` in binary. – VLAZ Nov 19 '21 at 21:16

0 Answers0