在 PHP 和 JS 中移位时得到不同的结果
我得到一些奇怪的结果,其中 2 个相同的函数(一个在 PHP 中,一个在 javascript 中)返回不同的结果。
这两行代码的输入是相同的:
a = 4653896912;
b = 13;
我仔细检查了变量类型,两个变量在 JS 中都是数字,在 PHP 中都是整数。
PHP 的代码行是这样的:
$a = $a >> $b;
对于 Javascript 来说是这样的:
a = a >> b;
你会期望 a 在两者之后具有相同的值,但我得到以下内容:
PHP: $a = 568102
JS: a = 43814
此时这让我完全困惑。
事实证明,这绝对是 PHP 使用 64 位整数而 JS 仅使用 32 位整数的问题。我现在面临的问题是我需要让 PHP 使用 32 位整数进行这些计算。我发现其他人编写的一个函数看起来应该可以工作,但它似乎根本没有改变我的输出。
private static function toInt32(&$x) {
$z = hexdec(80000000);
$y = (int) $x;
if($y ==- $z && $x <- $z){
$y = (int) ((-1) * $x);
$y = (-1) * $y;
}
$x = $y;
}
I'm getting some odd results where 2 identical functions (one in PHP and one in javascript) are returning different results.
The input for both of these lines of code is identical:
a = 4653896912;
b = 13;
I have double checked the variable types and both variables are numbers in JS and integers in PHP.
The line of code for PHP is this:
$a = $a >> $b;
For Javascript it's this:
a = a >> b;
You'd expect a to have the same value after both, but I'm getting the following:
PHP: $a = 568102
JS: a = 43814
Which has completely baffled me at this point.
Turns out this is definitely an issue of PHP using 64 bit integers and JS only using 32 bit. The problem I face now is that I need to get PHP to use 32-bit integers for these calculations. I found a function someone else wrote that looks like it should work, but it doesn't seem to be changing the output at all for me.
private static function toInt32(&$x) {
$z = hexdec(80000000);
$y = (int) $x;
if($y ==- $z && $x <- $z){
$y = (int) ((-1) * $x);
$y = (-1) * $y;
}
$x = $y;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面的代码演示了屏蔽数字的高 32 位以仅检索低 32 位以在计算中使用。 4294967295 是 2^32 - 1。我认为,如果您以这种方式屏蔽所有可能大于 32 位的值,那么您可以从 php 和 javascript 获得相同的结果。
The below code demonstrates masking the upper 32 bits of the number to retrieve only the lower 32 bits to use in your calculations. 4294967295 is 2^32 - 1. I think that if you mask all values that could be greater than 32 bits in this manner, then you can get the same results from your php and javascript.
4653896912 超过 32 位。可能会出现不可预测的结果。对于 PHP,我得到
$a = 43814
,但实际上是 358929617 >>> 13,所以 PHP 很可能执行 64 位操作,但 JavaScript 只是 32 位。4653896912 is more than 32 bits.. unpredictable results are likely. I get
$a = 43814
for PHP, but that is actually 358929617 >> 13, so in all likelihood PHP is doing 64 bit operations but JavaScript is only 32 bit.我相信这是因为您的
a
超出了 [PHP][1] 的 32 位有符号整数的限制。可能的最高值约为 200 万,
a
超过 40 亿。当您由于空间限制而翻滚时,结果可能是不可预测的(或者至少很难弄清楚)。
如果您的服务器使用 64 位版本的 PHP,那么它的最大值将远高于此,但 javascript 受到最终用户运行的限制。
您可以在他们的整数页面上阅读有关 PHP 的内容。
I believe it's because you're
a
is above the limit of a 32-bit signed integer for [PHP][1].The highest value possible is about 2 million, and
a
is over 4 billion.When you're rolling over because of space limitations, results can be unpredictable (or at least, very difficult to figure out).
If your server is on a 64-bit version of PHP then it'll max out much higher than than, but javascript is limited by what the end-user is running.
You can read up on PHP on their integers page.