I am working on BitWise AND operator in javascript.
I have two 32 bit nunber
4294901760 (11111111 11111111 00000000 00000000) and
4294967040 (11111111 11111111 11111111 00000000)
when I and them bitwise 4294901760 & 4294967040
I got -65536 as a result although the result should be 4294901760
.
Can any one please guide me am I missing something? Or what is the correct way to do it. Thanks
console.log((4294901760 & 4294967040) >>> 0);
Append >>> 0
to have it interpret your operation as unsigned.
Fiddle:
http://jsfiddle.net/JamZw/
More info:
Bitwise operations on 32-bit unsigned ints?
Operands of bitwise operations in javascript are converted to signed 32 bit integers. Unsigned 4294901760
has same binary representation as signed -65536
.
You can use >>> 0
to convert result of &
to unsigned, eg:
(4294901760 & 4294967040) >>> 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With