PHP Left Shift giving two answers on two different machines
I'm very confused about behaviour of PHP's left shift function. I'm using it on two different machines (dev and hosting), and they are giving me different answers. I've tracked it down to this calculation:
(-3941404251) << 5;
On one machine I'm getting the answer -1570884448; on the other, I get 0. On both systems, PHP_INT_MAX = 2147483647. The later is a 32-bit system, and the first a 64-bit, although php is running as a 32 bit process and still gives the same ans开发者_运维问答wer.
I can only assume that this is a problem with 32-bit vs 64-bit, but is there any easy way to get the desired behaviour. If somebody could point me to a function or something, that would be great.
Thanks!
First value is correct answer for your problem. Official manual says, that you can't use bitwise operator on number greater than max_int. So try using GMP functions (For example http://www.php.net/manual/en/function.gmp-and.php) and treat number as a string.
Are the machines running the same version of PHP? (-3941404251) is already too big for a 32 bit signed value, so I suspect the "correct" result is undefined and different versions/compilations/etc giving different results wouldn't be considered a bug.
You can use the BC Math functions to overcome the integer limitations. It's an alternative to elq's GMP function answer, and likely better supported.
精彩评论