assaf assaf - 1 month ago 4x
Javascript Question

Why is the bitwise AND of two of the same value producing a different value?

I have noticed a weird behaviour using bitwise AND operator in JS:

console.log((0xd41ddb80 & 0xd41ddb80).toString(16))

The result is -2be22480, but I was expecting 0xd41ddb80

What can be the cause of this behaviour?


From MDN

The operands of all bitwise operators are converted to signed 32-bit integers in two's complement format.

When interpreted as a signed 32-bit integer, the value 0xd41ddb80 represents the number -736240768. Using any bitwise operator on this number will coerce it into a signed 32-bit integer:

console.log(0xd41ddb80 & 0xffffffff)
console.log(0xd41ddb80 | 0)

The the base-16 equivalent of -736240768 is -2be22480, and that is what you are seeing .

You can observe similar behavior for any number greater than or equal to 0x80000000.