You are hereA 64 bit integer for PHP

A 64 bit integer for PHP


By Gerd Riesselmann - Posted on 27 January 2006

thegeek misses a 64 bit integer for PHP (German), since he uses a bitmask with more then 32 options. Here's a proposal, how to deal with this problem by splitting a float into two integers:

<?php
define("BIGINT_DIVIDER", 0x7fffffff + 1);

function split2Int(&$upper, &$lower, $value) {
$lower = intval($value % BIGINT_DIVIDER);
$upper = intval(($value - $lower) / BIGINT_DIVIDER);
}

function bigInt2float($upper, $lower) {
return $upper * BIGINT_DIVIDER + $lower;
}
?>

split2Int puts the upper 32 bits into $upper and the lower 32 bits into $lower. Binary operations can now be performed on both the ints. Afterwards, the result in converted to a float using bigInt2float(). I also wrapped up this stuff into a little class. You can download it here.

Another solution is the bigint pecl package:

http://pecl.php.net/package/big_int