PHP Left Shift gives two answers on two different machines

I am very confused about the behavior of the left shift function. I use it on two different machines (dev and hosting) and they give me different answers. I tracked it before this calculation:

(-3941404251) << 5;

On one machine, I get the answer -1570884448; on the other hand, I get 0. On both systems, PHP_INT_MAX = 2147483647. Later it is a 32-bit system and the first is 64-bit, although php works like a 32-bit process and still gives the same answer.

I can only assume that this is a problem with 32-bit and 64-bit, but is there an easy way to get the desired behavior. If someone can point me to a function or something else, that would be great.

Thank!

+5
source share
3 answers

The first value is the correct answer for your problem. The official guide says that you cannot use a bitwise operator with a number greater than max_int. So try using GMP functions (e.g. http://www.php.net/manual/en/function.gmp-and.php ) and treat the number as a string.

+2
source

Are machines with the same version of PHP used? (-3941404251) is already too large for a 32-bit signed value, so I suspect that the “correct” result is undefined, and different versions / compilations / etc giving different results will not be considered an error.

+1
source

BC Math . elq GMP , , .

+1
source

All Articles