Javascript:
var a=209348540618090 | 0;//-1050290838
PHP:
$a= 209348540618090|0;//209348540618090
$a=(209348540618090 & 0xffffffff) | 0; //3244676458
Why is the result of the bitwise operation in PHP different to the bitwise operation in Javascript?
Bitwise operators operate on 32-bit integers in JavaScript. Here's how it converts the integer before applying the OR operator:
2^32. Result is 3244676458.2^31 = 2147483648, substract 2^32. Result is -1050290838.-1050290838 and 0, result is -1050290838.Reference on how JS converts the number to a 32-bit integer: https://tc39.es/ecma262/multipage/abstract-operations.html#sec-toint32
On the other hand, the PHP example simple applies bitwise OR to the binary representation of the operands, which result in 3244676458.
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