Search code examples
integerunsigned

PHP's unsigned integer on 32 bit and 64 bit platform


Here is the code:

echo sprintf('%u',-123);

And this is the output on 32 bit platform:

4294967173

but on 64 bit:

18446744073709551493

How to make it the same ?Say,the int(10) unsigned


Solution

  • echo sprintf('-%u',abs(-123));
    

    or

    $n = -123;
    echo sprintf("%s%u", $n < 0 ? "-":"", abs($n));
    

    Though if you actually want to see the two's complement unsigned value of a negative number restricted to 32 bits just do:

    echo sprintf("%u", $n & 0xffffffff);
    

    And that will print 4294967173 on all systems.