How to Have a 64-Bit Integer in PHP

How can I have a 64-bit integer in PHP?

Native 64-bit integers require 64-bit hardware AND the 64-bit version of PHP.

On 32-bit hardware:

$ php -r 'echo PHP_INT_MAX;'
2147483647

On 64-bit hardware:

$ php -r 'echo PHP_INT_MAX;'
9223372036854775807

Pack and unpack 64 bit integer

Save it as two 32Bit instead:

$value = PHP_INT_MAX;
$highMap = 0xffffffff00000000;
$lowMap = 0x00000000ffffffff;
$higher = ($value & $highMap) >>32;
$lower = $value & $lowMap;
$packed = pack('NN', $higher, $lower);

list($higher, $lower) = array_values(unpack('N2', $packed));
$originalValue = $higher << 32 | $lower;
  • Algorithm taken from: http://php.net/pack#109328
  • Example: http://codepad.viper-7.com/UuLmor

How to use a 32bit integer on a 64bit installation of PHP?

Integers are the size of pointers on that platform. (32-bit PHP --> 32-bit integers. 64-bit PHP --> 64-bit integers).

Note that when integer operations overflow, the variables become floats. The PHP documentation explains all of this well.

I'm not sure what you're doing in your code that would cause you to care what size the integer is though. If you only care about 32-bits of a value, however, you can always mask off the low 32 bits:

$val = $something & 0xFFFFFFFF;

base64 encoding of binary value of 64 bit integer in PHP is not working

Update

Try using gmp_export instead of pack. Make sure to pass the integer as a string to gmp_init since it will overflow otherwise:

$t = gmp_init("11545152599186258990");
$byte_array_t = gmp_export($t, 8);
echo base64_encode($byte_array_t); // LrwswB6fOKA=

Demo

PHP: Converting a 64bit integer to string

You're losing the precision on the assignment, not on the string conversion. If this variable's value is actually hardcoded, and you can't change that, there's nothing you can do.

A line like:

$i = 76561197961384956;

will always lose precision. If you need to keep the whole thing, store it into a string, you can't store it as an int like that and keep all the digits.

Is it possible to use a large unsigned int64 without losing precision in PHP?

Yes it is a limitation of PHP and there is nothing you can do about it short of recompiling your PHP interpreter. Even then you are limited to the types your native system supports which wouldn't be bigger than 64bit normally. You can, as you know, use GMP, or BCMath, but that is not what your asking.

Under the hood, depending on your system the PHP integer and PHP floating point types corresponds to a signed C integer type, and C float types (PHP always uses C doubles for 'floats' AFAIK). This is a static relationship and can't change after compile time. Since the C types have fixed precision of course the PHP ones do too.

The "overflow" into a float is just a convenient compromise so you can store really big numbers, rather than not at all. Your losing some precision yeah, but only in the significand. PHP is not going to automatically convert the number to some other bigger precision floating point format because it doesn't have one.



Related Topics



Leave a reply



Submit