简体   繁体   English

Javascript 和 PHP Xor 等效项

[英]Javascript & PHP Xor equivalent

I have a javascript code:我有一个 javascript 代码:

var c = 267414715;
var d = c ^ ("0x81BE16CD");

Result is -1907459466结果是-1907459466

http://jsfiddle.net/4N3JY/1/ http://jsfiddle.net/4N3JY/1/

I can't seem to get a PHP equivalent.我似乎无法获得等效的 PHP。 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";
?>

Output:输出:

With hexdec: 2387507830
With base_convert(2): 9223372036587361092
With base_convert(10): 2387507830

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).有人可以指出正确的等效代码,并解释不同版本(base_convert / hexdec /“正确”等效在其工作中的不同之处)。

2387507830 == -1907459466 when using unsigned integers (look at the bit values of the least significant bits) 2387507830 == -1907459466 使用无符号整数时(查看最低有效位的位值)

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千1110 0100 1110 0111 1010 0111 0110

your problem is a 32 bit roll over.您的问题是 32 位翻转。 To compensate you can simply & 0xffffffff which will 0 out the most significant 32 bits, and make both answers the same.为了补偿,您可以简单地 & 0xffffffff 它将最高有效的 32 位清零,并使两个答案相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM