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,则

...

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

  1. java 大整数编程_Java编程--RSA算法中的大整数运算

    Java编程–RSA算法中的大整数运算 RSA原理浅析 RSA是利用陷门单向函数实现的,其安全基础依赖于大整数的分解问题的难解性 算法过程 为了加深对RSA算法的了解,接下来通过简单的一个例子来分析一 ...

  2. java二进制数组_Java中的二进制及基本的位运算

    Java中的二进制及基本的位运算 二进制是计算技术中广泛采用的一种数制.二进制数据是用0和1两个数码来表示的数.它的基数为2,进位规则是"逢二进一",借位规则是"借一当二 ...

  3. java int位运算_java中int的表示方法,位运算的解析

    随着时代的不断发展,越来越多的人开始投入到java的学习当中来.今天就来为大家介绍java中int的表示方法以及位运算的内容有哪些一起来看看吧. 首先我们需要知道的是,在java中,int是使用32位 ...

  4. 《算法竞赛进阶指南》打卡-基本算法-AcWing 90. 64位整数乘法:位运算

    文章目录 题目解答 题目链接 题目解答 分析: 位运算,快速幂的思想 /* a * b a + a + a + a + a ... + a 共b个aa * 1 = a a * 2 = 2a a * 4 ...

  5. LeetCode 371. 两整数之和(位运算加法)

    1. 题目 不使用运算符 + 和 - ​​​​​​​,计算两整数 ​​​​​​​a .b ​​​​​​​之和. 示例 1: 输入: a = 1, b = 2 输出: 3示例 2: 输入: a = -2 ...

  6. 计算机中逻辑运算顺序,【计算机系统】位运算与逻辑运算

    计算机系统的位运算与逻辑运算 一.位 1.定义 二进制数字系统中数据存储的最小单位,即每个二进制数0或1就称为位.位也叫比特(bit),8个bit组成一个字节(byte),每个字节表示程序中的某些文本 ...

  7. 计算一个二进制数中数字“1”的个数(位运算)

    int numberOfOne( unsigned value ) {int count;for( count = 0; value != 0; value >>= 1 )if( ( va ...

  8. 小米oj 反向位整数(简单位运算)

     反向位整数 序号:#30难度:一般时间限制:1000ms内存限制:10M 描述 输入32位无符号整数,输出它的反向位. 例,输入4626149(以二进制表示为00000000010001101001 ...

  9. 程序员面试金典 - 面试题 05.06. 整数转换(位运算)

    1. 题目 整数转换.编写一个函数,确定需要改变几个位才能将整数A转成整数B. 示例1:输入:A = 29 (或者0b11101), B = 15(或者0b01111)输出:2示例2:输入:A = 1 ...

最新文章

  1. 2008-2018,5个版本互联网大脑模型的演进与对比
  2. 为Android运行新的英特尔模拟器
  3. OTT交付如何超越传统广电交付,为用户带来高质量视频网络——对话Synamedia流媒体技术发展经理卢彦林...
  4. Spark笔记:RDD基本操作(下)
  5. Centos 7 KVM安装win10
  6. 在 k8s 以外的分布式环境中使用 Dapr
  7. 第二阶段冲刺10天 第六天
  8. Linux脚本验证的常见方法,linux shell常用循环与判断语句(for,while,until,if)使用方法...
  9. 正在运行的程序和数据都是保存在计算机的,正在运行的程序和数据是暂在计算机的硬盘中吗?...
  10. 不限流量的物联卡是否真存在
  11. [jquery]高级篇--获取div子元素
  12. 「经典题」完整的前端项目开发流程
  13. 70.(cesium之家)cesium接入天地图影像与注记(经纬度)
  14. ZeroC Ice Hello World
  15. 2022年G3锅炉水处理考试题库及模拟考试
  16. Python 处理表格进行成绩排序的操作代码
  17. android背景图拉伸,解决android:background背景图片被拉伸问题
  18. linux 上 gcc -m32 编译报错解决方案
  19. 【格言精选】影视剧情大俗套——你注意到多少
  20. 【英语学习工具】解说 LeHoCat 提供免费的 视频集制作工具 使用方法, 看视频学英语的制作工具, 制作英语教学课件的工具, 帮助自学英语(详细图文解说)

热门文章

  1. 【MATLAB appdesigner】14_app界面“运行”小技巧总结(非常干)
  2. 网络存储技术Windows server 2012 (项目四 存储池的高级配置与管理)
  3. java数组游戏_基于java的挖地雷游戏
  4. 蝙蝠侠:黑暗骑士崛起 离线版(含数据包) v1.1.1
  5. ubuntu 编译zbar
  6. html 一键复制 ios,兼容安卓和ios实现一键复制内容到剪切板
  7. android 9.0谷歌商店,Android 10现可选择Play商店主题模式 附Android 9强制开启深色模式教程...
  8. flutter展示图片
  9. 3.Android高仿网易云音乐-首页复杂发现界面布局和功能
  10. 学习笔记——高阶函数