when I tried this
~1099511627520
on 32bit OS,the result is negative but the 64bit is OK~ So how to make it work on 32bit OS?
anyone can help me? thanks~
when I tried this
~1099511627520
on 32bit OS,the result is negative but the 64bit is OK~ So how to make it work on 32bit OS?
anyone can help me? thanks~
Using BC Math functions you can get bitwise negations of 64bit numbers on 32bit machines like:
<?php
define('MAX_UINT_64', '18446744073709551615');
$number_64bit = '1099511627520';
$negate_64bit = bcsub(MAX_UINT_64, $number_64bit, 0);
var_dump($negate_64bit);
?>
It outputs:
18446742974197924095
You may try to use bcmath functions.
function neg($number)
{
return bcsub(0, bcadd($number, 1));
}
echo neg('1099511627520') . PHP_EOL; // -1099511627521
echo ~1099511627520 . PHP_EOL; // -1099511627521
echo neg('1') . PHP_EOL; // -2
echo ~1 . PHP_EOL; // -2
echo neg('-1099511627520') . PHP_EOL; // 1099511627519
echo ~-1099511627520 . PHP_EOL; // 1099511627519
echo neg('-1') . PHP_EOL; // 0
echo ~-1 . PHP_EOL; // 0