views:

300

answers:

3

Hello!

Does anyone know of a class/library/etc. that can simulate 32 bit unsigned integers on a 32 bit platform in PHP? I'm porting a C lib into PHP and it uses a lot of integers that are greater than the maximum for 32 bit signed int.

A: 

Why can't you write

  modulo(a+b,2^32)

or its PHP equivalent for a, b being "unsigned" integers you are trying to "unsigned integer add", similarly for subtract, multiply, divide, ...? You can of course hide this in a function add_unsigned to make the code a bit clearer.

Ira Baxter
+2  A: 

Try the BC Math Library, which "supports numbers of any size and precision, represented as strings."

Good luck!

Mike Cialowicz
I tried that and it works perfectly for some operations. I guess I will have to write my own functions for some bitwise operations and pack/unpack.
Zeta Two
A: 

Do you really need it? php will convert to float if you go out of bounds of an int.

http://www.php.net/manual/en/language.types.integer.php

var_dump(PHP_INT_MAX);
$int = PHP_INT_MAX;
var_dump($int + $int);
chris