PHP Bitwise XOR与JavaScript Bitwise异或 [英] PHP Bitwise XOR vs. JavaScript Bitwise XOR

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

问题描述

我正在尝试找到一种方法来使 PHP Bitwise XOR 结果与 JavaScript的结果相匹配XOR 。我遇到了这个问题的不同问题,而且都没有答案。以下是其中一些:

I'm trying to find a way to make PHP Bitwise XOR results match the results of JavaScript Bitwise XOR. I came across different questions of this issue, and all without answers. Here are a few of them:

Javascript& ; PHP Xor等价物

php bitwise XOR和js bitwise XOR产生不同的结果

JS bitwise XOR运算符的行为与PHP的对应物。如何获得与PHP返回相同的结果?

我知道PHP使用64位而不是32位JavaScript,但我的问题是,是否存在任何手动方式来计算类似的结果?我们怎样才能使PHP得到与JS类似的结果?

I know that PHP is using 64 bit compared to 32 bit JavaScript, but my question is, is there any manual way to count similar results? How can we make PHP get similar results as JS?

如果数字很短,JS和PHP的结果总是相同的,但是,如果数字是很久以来,问题就发生了。示例:

If the numbers are short, the results will always be the same in JS and PHP, However, if the numbers are long, the issue happens. Example:

var a = 234324234232;
var b = 221312312232;
console.log(a^b);

JS输出:

166587472

PHP代码:

$a = 234324234232;
$b = 221312312232;
echo $a^$b;

PHP输出:

21641423952






有时JavaScript会给出否定结果:


Sometimes JavaScript gives negative results:

var a = 202338273;
var b = 523511134400;
console.log(a^b);

JS输出

-272722143

PHP代码:

$a = 202338273;
$b = 523511134400;
echo $a^$b;

PHP输出:

523713287969


推荐答案

面具签名int。

$c = ($a ^ $b) & 0xffffffff;
if ($c & 0x80000000)
  $c -= 0x100000000;
echo $c;

这篇关于PHP Bitwise XOR与JavaScript Bitwise异或的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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