2

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~

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
Leo
  • 629
  • 1
  • 9
  • 25

2 Answers2

3

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

जलजनक
  • 3,072
  • 2
  • 24
  • 30
0

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
sectus
  • 15,605
  • 5
  • 55
  • 97