PHP异或运算两个数字 [英] PHP XOR operation two numbers

查看:44
本文介绍了PHP异或运算两个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对两个如下所示的值进行异或:

I am trying to xor two values which are like below:

变量 1:6463334891变量 2:1000212390

Variable 1 : 6463334891 Variable 2 : 1000212390

当我在 php 中对这些值进行异或运算时,它给了我错误的答案.

When i did xor with these values in php it gives me wrong answer.

它应该给我7426059853"

It should give me "7426059853"

这是我的代码

 $numericValue = (int)$numericValue;
 $privateKey = (int)$privateKey;
 echo  "Type of variable 1 ".gettype($numericValue)."<br />";
 echo  "Type of variable 2 ".gettype($privateKey)."<br />";
 $xor_val = (int)$numericValue ^ (int)$privateKey;
 echo "XOR Value :".$xor_val."<br />";

推荐答案

那是因为您达到了 MAXIMUM INTEGER LIMIT2147483647

That is because you re hitting the MAXIMUM INTEGER LIMIT which is 2147483647

最大值取决于系统.32位系统最大-2147483648 到 2147483647 的有符号整数范围.例如这样的系统, intval('1000000000000') 将返回 2147483647.64 位系统的最大有符号整数值为9223372036854775807.

The maximum value depends on the system. 32 bit systems have a maximum signed integer range of -2147483648 to 2147483647. So for example on such a system, intval('1000000000000') will return 2147483647. The maximum signed integer value for 64 bit systems is 9223372036854775807.

因此要处理如此大的整数,您需要使用<代码>(GMP)GNU多精度

Thus to handle such big integers you need to make use of an extension like (GMP) GNU Multiple Precision

<?php
$v1="6463334891";
$v2="1000212390";
$a = gmp_init($v1);
$b = gmp_init($v2);
echo gmp_intval($a) ^ gmp_intval($b); //"prints" 7426059853

否则,切换到 64 位系统.

Else , Switch to a 64-bit system.

这篇关于PHP异或运算两个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆