KALPESH GHAG - 1 year ago 108

PHP Question

I am trying to xor two values which are like below:

Variable 1 : 6463334891

Variable 2 : 1000212390

When i did xor with these values in php it gives me wrong answer.

It should give me "7426059853"

This is my code

`$numericValue = (int)$numericValue;`

$privateKey = (int)$privateKey;

echo "Type of variable 1 ".gettype($numericValue)."<br />";

echo "Type of variable 2 ".gettype($privateKey)."<br />";

$xor_val = (int)$numericValue ^ (int)$privateKey;

echo "XOR Value :".$xor_val."<br />";

Answer Source

That is because you re hitting the ** MAXIMUM INTEGER LIMIT** which is

`2147483647`

The maximum value depends on the system. 32 bit systems have a maximum signed integer range of -2147483648 to 2147483647. So for example on such a system, intval('1000000000000') will return 2147483647. The maximum signed integer value for 64 bit systems is 9223372036854775807.

Thus to handle such big integers you need to make use of an extension like `(GMP) GNU Multiple Precision`

```
<?php
$v1="6463334891";
$v2="1000212390";
$a = gmp_init($v1);
$b = gmp_init($v2);
echo gmp_intval($a) ^ gmp_intval($b); //"prints" 7426059853
```

Else , Switch to a `64-bit system.`