Reputation: 31
How I can get result -1098394484 on 64bit machine in PHP:
32b
echo 1049089164 ^ 2147483648 ^ 0 ^ 0; // => -1098394484
64b
echo 1049089164 ^ 2147483648 ^ 0 ^ 0; // => 3196572812, but need -1098394484
Upvotes: 2
Views: 682
Reputation: 897
Try it:
1 049 089 164 = 0b00111110100001111101010010001100
2 147 483 648 = 0b10000000000000000000000000000000
XOR:
= 0b10111110100001111101010010001100
3 196 572 812 = 0b10111110100001111101010010001100 -> OK, if 64 bit
-1 098 394 484 = 0b10111110100001111101010010001100 -> OK, if 32-bit two complement
So actually both are good! Anyway, VAR ^ 0 ^ 0 = VAR (because two xors are idempotent together).
Anyway, use 0xABCDEF09 (hexa) format on the output. Try this on both (unfortunately I cannot do for you on 32bit):
echo 1049089164 ^ 2147483648; // => -1098394484
echo "<br/>";
echo 1049089164 ^ 2147483648 ^ 0 ^ 0; // => -1098394484
echo "<br/>";
echo dechex(1049089164 ^ 2147483648); // => 3196572812, but need -1098394484
echo "<br/>";
echo dechex(1049089164 ^ 2147483648 ^ 0 ^ 0); // => 3196572812, but need -1098394484
outputs:
3196572812
3196572812
be87d48c
be87d48c
Or you can try here: http://writecodeonline.com/ In PHP5, you get the output I wrote, but in PHP4, you get -1098394484. Use echo PHP_INT_SIZE; to determine the architecture (4 means 32bit and 8 means 64bit, that site is 64bit in php5 and 32bit in php4).
So what you really want to force the INT to 4 bytes on both architectures. Refer to another SO question: Force PHP integer overflow
function intval32bits($value)
{
$value = ($value & 0xFFFFFFFF);
if ($value & 0x80000000)
$value = -((~$value & 0xFFFFFFFF) + 1);
return $value;
}
echo intval32bits(1049089164 ^ 2147483648 ^ 0 ^ 0);
So you have your solution.
Upvotes: 3