Javascript& PHP Xor等价 [英] Javascript & PHP Xor equivalent
问题描述
我有一个javascript代码:
I have a javascript code:
var c = 267414715;
var d = c ^ ("0x81BE16CD");
结果是 -1907459466
我似乎无法获得PHP等价物。尝试了以下内容:
I can't seem to get a PHP equivalent. Have tried the following:
<?php
$c=267414715;
$d=$c ^ hexdec("0x81BE16CD");
echo "With hexdec: $d\n";
$d=$c ^ base_convert("0x81BE16CD", 16, 2);
echo "With base_convert(2): $d\n";
$d=$c ^ base_convert("0x81BE16CD", 16, 10);
echo "With base_convert(10): $d\n";
?>
输出:
With hexdec: 2387507830
With base_convert(2): 9223372036587361092
With base_convert(10): 2387507830
有人可以指出正确的等效代码,并解释不同版本(base_convert / hexdec /correct等效的工作方式不同)。
Can someone please point out the correct equivalent code, and also explain how the different versions (base_convert / hexdec / "correct" equivalent differ in their working).
推荐答案
2387507830 == -1907459466使用无符号整数时(查看最低有效位的位值)
2387507830 == -1907459466 when using unsigned integers (look at the bit values of the least significant bits)
2387507830 = 0000 0000 0000 0000 0000 0000 0000 0000 1000 1110 0100 1110 0111 1010 0111 0110
-1907459466 = 1111 1111 1111 1111 1111 1111 1111 1111 1000 1110 0100 1110 0111 1010 0111 0110
2387507830 = 0000 0000 0000 0000 0000 0000 0000 0000 1000 1110 0100 1110 0111 1010 0111 0110 -1907459466= 1111 1111 1111 1111 1111 1111 1111 1111 1000 1110 0100 1110 0111 1010 0111 0110
您的问题是32位翻转。为了补偿你可以简单地和& 0xffffffff将最高32位的0,并使两个答案相同。
your problem is a 32 bit roll over. To compensate you can simply & 0xffffffff which will 0 out the most significant 32 bits, and make both answers the same.
这篇关于Javascript& PHP Xor等价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!