douzhuo2002 2014-06-11 03:55
浏览 86
已采纳

Javascript和PHP Xor相当于

I have a javascript code:

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

Result is -1907459466

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

I can't seem to get a PHP equivalent. Have tried the following:

<?php    
$c=267414715;

$d=$c ^ hexdec("0x81BE16CD");
echo "With hexdec: $d
";

$d=$c ^ base_convert("0x81BE16CD", 16, 2);
echo "With base_convert(2): $d
";

$d=$c ^ base_convert("0x81BE16CD", 16, 10);
echo "With base_convert(10): $d
";
?>

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).

  • 写回答

1条回答 默认 最新

  • doubaran2438 2014-06-11 04:37
    关注

    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

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)