问题描述
我正在尝试找到一种方法来使 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:
我知道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异或的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!