在php中将5按位与运算,PHP 5.2和PHP 5.3中对大整数的按位运算(Bitwise operations on big integers in PHP 5.2 and PHP 5.3)...

PHP 5.2和PHP 5.3中对大整数的按位运算(Bitwise operations on big integers in PHP 5.2 and PHP 5.3)

我将省略有关我是如何做到这一点的细节,但重点是下面的代码在PHP 5.2 x86和PHP 5.3 x86上产生不同的结果。

$result = (((-1035721791 + -2004767255) ^ 3402727701) + 4148612726) ^ -759268493;

echo 'Platform: ' . php_uname(), PHP_EOL;

echo 'PHP version: ' . PHP_VERSION, PHP_EOL;

echo 'Max integer: ' . PHP_INT_MAX, PHP_EOL;

echo 'Result: ' . $result, PHP_EOL;

结果是:

Linux x64,PHP 5.3

Platform: Linux Test1 3.2.0-25-generic #40-Ubuntu SMP Wed May 23 20:30:51 UTC 2012 x86_64

PHP version: 5.3.10-1ubuntu3.2

Max integer: 9223372036854775807

Result: -1511693242

Linux x86,PHP 5.3

Platform: Linux Test2 2.6.32-5-686 #1 SMP Sun May 6 04:01:19 UTC 2012 i686

PHP version: 5.3.3-7+squeeze9

Max integer: 2147483647

Result: -1511693242

Windows x86,PHP 5.3

Platform: Windows NT Test3 6.1 build 7600 i586

PHP version: 5.3.6

Max integer: 2147483647

Result: -1511693242

Linux x86,PHP 5.2

Platform: Linux Test4 2.6.18-8.el5 #1 SMP Thu Mar 15 19:57:35 EDT 2007 i686

PHP version: 5.2.10

Max integer: 2147483647

Result: -1868155656

最后的结果与其余部分不同,因为我有些不为人知。

如您所见,表达式-1035721791 + -2004767255的结果不适合int32,但PHP 5.3在x86和x64平台上产生相同的结果。 PHP 5.2在x86上产生错误的结果。

是否记录了这种差异? 它是PHP的一个功能还是PHP 5.2的错误?

I'll omit the details of how I came to this but the point is that the code below produces different results on PHP 5.2 x86 and PHP 5.3 x86.

$result = (((-1035721791 + -2004767255) ^ 3402727701) + 4148612726) ^ -759268493;

echo 'Platform: ' . php_uname(), PHP_EOL;

echo 'PHP version: ' . PHP_VERSION, PHP_EOL;

echo 'Max integer: ' . PHP_INT_MAX, PHP_EOL;

echo 'Result: ' . $result, PHP_EOL;

The results are:

Linux x64, PHP 5.3

Platform: Linux Test1 3.2.0-25-generic #40-Ubuntu SMP Wed May 23 20:30:51 UTC 2012 x86_64

PHP version: 5.3.10-1ubuntu3.2

Max integer: 9223372036854775807

Result: -1511693242

Linux x86, PHP 5.3

Platform: Linux Test2 2.6.32-5-686 #1 SMP Sun May 6 04:01:19 UTC 2012 i686

PHP version: 5.3.3-7+squeeze9

Max integer: 2147483647

Result: -1511693242

Windows x86, PHP 5.3

Platform: Windows NT Test3 6.1 build 7600 i586

PHP version: 5.3.6

Max integer: 2147483647

Result: -1511693242

Linux x86, PHP 5.2

Platform: Linux Test4 2.6.18-8.el5 #1 SMP Thu Mar 15 19:57:35 EDT 2007 i686

PHP version: 5.2.10

Max integer: 2147483647

Result: -1868155656

The last result is different from the rest for some unknown for me reason.

As you can see, the result of expression -1035721791 + -2004767255 doesn't fit int32 but PHP 5.3 produces the same result both on x86 and x64 platforms. And PHP 5.2 produces incorrect result on x86.

Is this difference documented? Is it a feature of PHP or a bug of PHP 5.2?

原文:https://stackoverflow.com/questions/11402469

更新时间:2020-02-24 20:23

最满意答案

当PHP遇到一个不适合PHP_INT_MAX(x86上为32位) 的数字时 , 它会将数字转换为浮点数 :

如果PHP遇到超出整数类型边界的数字,它将被解释为float。 此外,导致超出整数类型边界的数字的操作将返回浮点数。

更改浮点行为以在所有平台和所有编译器上始终使用双精度。 (Christian Seiler)

虽然我不能100%确认这是你所看到的原因,但我想它应该与它有关。

无论如何,如果你正在处理那些大数字,你应该停止依赖浮点精度并使用提供大量数字支持的扩展,例如BC Math或GMP

When PHP encounters a number which doesn't fit in PHP_INT_MAX (32bits on x86), it will convert the number into a float:

If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.

Additionally, PHP 5.3 changelog says:

Changed floating point behaviour to consistently use double precision on all platforms and with all compilers. (Christian Seiler)

While I can't confirm you 100% that this is the reason for what you are seeing, I suppose it should have something to do with it.

Anyway, if you are dealing with those sorts of big numbers, you should stop relying on float precision and use an extension offering large numbers support instead, such as BC Math or GMP

2012-07-09

相关问答

PHP.net提供了从PHP 5.2.x升级到PHP 5.3的指南。 这包括向后兼容的一节。 从我的经验来看,从5.2到5.3的过渡非常顺利。 我的应用程序遇到的唯一问题是确保我的DateTime设置属性是在我的php.ini中配置的,并过滤出一些开始出现的过分贬低警告。 PHP.net features a guide for upgrading from PHP 5.2.x to PHP 5.3. This includes a section on backwards compatibili

...

来自: http : //www.php.net/archive/2010.php#id2010-07-22-1 此版本标志着对PHP 5.2的主动支持的结束。 在此版本之后,PHP 5.2系列将不会再获得进一步的主动错误维护。 针对PHP 5.2的安全修补程序可能会逐个发布。 从那以后,我们已经看到了另外三个版本的5.2系列主要关注安全补丁。 据我所知,不能保证这些补丁将在2012年之前发布。 From: http://www.php.net/archive/2010.php#id2010-07

...

function whatever($points, $pdf) {

$op = 'f';

$h = $pdf->h;

$k = $pdf->k;

$points_string = '';

for($i=0; $i < 8; $i+=2){

$points_string .= sprintf('%.2F %.2F', $points[$i]*$k,($h-$points[$i+1])*$k);

...

答案是寻找替代解决方案。 PHPBrew和众多论坛阅读简单地切断了5.2作为旧技术和浪费时间支持这么少的用户。 我也浪费了足够的时间,并转向了没有5.2的替代解决方案。 The answer was to look for an alternate solution. PHPBrew and numerous forums read have simply cut off 5.2 as old technology and a waste of time supporting for so few

...

您可以在PHP.net上找到从5.2到5.3的向后不兼容更改的详尽列表 。 PHP扩展依赖于phpapi版本,以及Zend Module API版本和Zend Extension API版本,据我所知,由于对该语言所做的核心更改,每个PHP版本都会发生变化。 这解释了为什么每个PHP版本都有一个针对每个扩展的新构建。 You can find an exhaustive list of backwards incompatible changes from 5.2 to 5.3 on PHP.ne

...

内部实现应该与外部OS实现完全兼容。 假设OS实现(libxcrypt?)有成本参数的默认值(如果没有提供) - 你只需要追踪它是什么! The internal implementations are supposed to be completely compatible with the external OS implementations. Presumably the OS implementation (libxcrypt?) has a default value for the

...

找到了我自己对这个问题的答案: class Product_Sub_SomeClass {

...

}

// Alternative PHP 5.3+ namespace\class

if (function_exists('class_alias')) {

class_alias('Product_Sub_SomeClass', 'Product\Sub\SomeClass');

}

Found my own answer to this problem: class Pro

...

我想我应该更新并改进这个答案,因为我在过去几年里学到了很多关于密码散列的知识。 PHP 5.5版将提供一种使用BCrypt的便捷方式,对于PHP 5.3.7及更高版本,存在兼容包。 请看看这个答案 。 对于5.3之前的PHP版本,建议使用phpass库,它们支持PHP回到版本3。 I think i should update and improve this answer, because i learned a lot about password hashing in the last ye

...

当PHP遇到一个不适合PHP_INT_MAX(x86上为32位) 的数字时 , 它会将数字转换为浮点数 : 如果PHP遇到超出整数类型边界的数字,它将被解释为float。 此外,导致超出整数类型边界的数字的操作将返回浮点数。 另外, PHP 5.3更新日志说: 更改浮点行为以在所有平台和所有编译器上始终使用双精度。 (Christian Seiler) 虽然我不能100%确认这是你所看到的原因,但我想它应该与它有关。 无论如何,如果你正在处理那些大数字,你应该停止依赖浮点精度并使用提供大量数字支持

...

发生这种情况的原因是因为在5.3版本中,PHP开发人员决定要求在PHP.ini文件(或.htaccess中,或使用date_default_timezone_set() )中明确设置时区。 之所以这样做,是因为早期的PHP版本在未设置时区和人们依赖默认设置(通常实际上并不正确)(例如,因为他们在不同时区使用托管服务器)而出现问题。 由此造成了大量严重的问题,并且要求明确设置是试图解决这些问题。 解决方案是确保您的PHP.ini文件包含时区设置。 如果您使用的是共享主机并且无法更新PHP.ini,则

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值