开发者

Hexadecimal XOR with 2 values in PHP and JavaScript

开发者 https://www.devze.com 2023-04-05 15:29 出处:网络
I can\'t find the solution for very easy qu开发者_运维技巧estion. Code in JavaScript: var x = -1 ^ 0xF00F9344;

I can't find the solution for very easy qu开发者_运维技巧estion.

Code in JavaScript:

var x = -1 ^ 0xF00F9344;

In this case x value is 267414715

Code in PHP:

$x = -1 ^ 0xF00F9344;

The result is -4027552581

Any idea, how to get 267414715 (correct) result in PHP?


While javascript bit operations are always 32-bit, php depends on the platform's word size:

on a 32-bit platform

$n    = "11110000000011111001001101000100"
-1^$n = "00001111111100000110110010111011"  = 267414715

on a 64 bit platform

$n    = "0000000000000000000000000000000011110000000011111001001101000100"
-1^$n = "1111111111111111111111111111111100001111111100000110110010111011" = -4027552581
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号