By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,442 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

maximum integer size?

P: n/a
I'm trying to figure out how big integers in PHP can be and am having
some difficulty.

My first idea was to try something like this:

<?
for ($bits=0; (1<<($bits+1)) > (1<<$bits); $bits++);
echo $bits;
?>

The result was 30. This, however, is incorrect as the following
demonstrates:

<?
$num = 1<<30;

echo "num = $num\r\n";
echo 'num<<1 = '.($num<<1)."\r\n";
echo 'num*2 = '.($num*2)."\r\n";
?>

That my first idea would return the wrong result doesn't surprise me,
though, since bitwise operators in PHP are treated as arithematic ones
and not logical ones.

So I came up with another test.

<?
for ($bits=0; pow(2,$bits+1) > pow(2,$bits); $bits++);
echo $bits;
?>

In Windows XP, I got 1024 back. On Linux 2.6.13.2 and 2.4.28 (as
reported by phpinfo), all I get are timeout errors. So I decided to
make my script more efficient and hopefully faster:

<?
$x = 2;
$y = 1;
for ($bits=1; ($x*=2) > ($y*=2); $bits++);
echo $bits;
?>

On Windows XP, I again got 1024 back, whereas on Linux, I again got
timeout errors.

All in all, this leaves me a little confussed as to how to interpret
the results. Can integers in PHP be of an infinite precission when
running on Linux? If so, what's the point of the BCMath / GMP
functions on Linux?

Also, if this is the case, then how would I go about writting an
infinite precission integer arithmetic library that wasn't OS
dependant? I could make it work on Windows by using the bcmath
functions, since Windows builds of PHP include them by default, and use
PHP's normal integer stuff on Linux (Linux builds of PHP don't include
bcmath by default). But what about all the other platforms for which
PHP is available on? What about Mac OS X / OS/2 / etc? Would it be
safe to assume that if PHP's built-in integer functions didn't support
infinite precission that it'd include support for bcmath?

Feb 16 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In answer to you linux time out problem, run it on the command line
then it wont turn out. This command should do the trick I think: php -a
[file path, not remote path].

I think the length of the integer is dependant on the number of bits
the computer is so 32bit or 64 bit. You are running a 32bit computer,
yes? So 32 squared (1024) is the length, whereas if you were using
64bit then it would be 64 squared (4096)

Feb 16 '06 #2

P: n/a
yawnmoth wrote:
I'm trying to figure out how big integers in PHP can be and am having
some difficulty. From http://us3.php.net/intval :


"The maximum value depends on the system. 32 bit systems have a maximum
signed integer range of -2147483648 to 2147483647. So for example on
such a system, intval('1000000000000') will return 2147483647. The
maximum signed integer value for 64 bit systems is 9223372036854775807."

Feb 16 '06 #3

P: n/a
Michael wrote:
In answer to you linux time out problem, run it on the command line
then it wont turn out. This command should do the trick I think: php -a
[file path, not remote path].

I think the length of the integer is dependant on the number of bits
the computer is so 32bit or 64 bit. You are running a 32bit computer,
yes? So 32 squared (1024) is the length, whereas if you were using
64bit then it would be 64 squared (4096)


Since you are requesting this in the PHP newsgroup, a search for INTEGER in the
PHP docs yields:

http://us2.php.net/intval

"The maximum value depends on the system. 32 bit systems have a maximum signed
integer range of -2147483648 to 2147483647. So for example on such a system,
intval('1000000000000') will return 2147483647. The maximum signed integer value
for 64 bit systems is 9223372036854775807."
Another point is that may have more to do with the OS you are on. So, if you
are running a 32bit OS on a 64bit CPU, the max integer value will still be
2147483647. It really has more to do with the compilers, math libraries and OS
limitations as well as those of the cpu type.

--
Michael Austin.
Consultant
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)
Feb 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.