Question

Lets assume we are talking about 32bit system.

PHP doesn't support unsigned INT. It means that INT value should be between -2,147,483,648 and 2,147,483,647 values. And INT takes 4 bytes to store a value which are 32 bits length.

So does it mean that I have only 31 bits for value and 1 bit for sign? Or I can use whole 32 bits to store a value?

Was it helpful?

Solution

You are using the whole 32 bits. It's just that the default output functions interpret it as signed integer. If you want to display the value "raw" use:

printf("%u", -1);    //  %u for unsigned

Since PHP handles the integers signed internally however, you can only use bit arithmetics, but not addition/multiplication etc. with them - if you expect them to behave like unsigned ints.

OTHER TIPS

As far as i know on a 32-bit system the largest positive integer possible is 2147483647 values above will be float values, since a float value in php can take values up to 10000000000000.

First of all, if you want to do calculations with huge numbers (say, regulary greater than 10k), you should use the bcmath arbitrary precision module.

Secondly, the official php implementation uses the Two's complement internally to represent numbers, like virtually all other compilers and interpreters. Since the entropy of the signed bit (if you count 0 as positive[1]) is 1, and the entropy of 31 bits is, well, 31, you can store 232 = 4 294 967 296 distinct values. How you interpret them is up to your application.


[1] - 0 is neither positive nor negative.

Normally with 32 bit integers the difference between signed and unsigned is just how you interpret the value. For example, (-1)+1 would be 1 for both signed and unsigned, for signed it's obvious and for unsigned it's of course only true if you just chop the overflow off. So you do have 32 bits to store values, it just happens to be so that there's 1 bit that is interpreted differently from the rest.

Two's complement is most frequently used to store numbers, which means that 1 bit is not wasted just for the sign.

http://en.wikipedia.org/wiki/Two's_complement

In PHP if a number overflows the INT_MAX for a platform, it converts it to a floating point value.

2147483647 is the usual value 2^31-1. 1 bit for sign and -1 because we also represent 0.

from the manual: "The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5."

Yes if it would have used unsigned integer it will use 32 bit to store it as you don't need sign in that case but as it supports only signed integers a 32 bit systems will have 31 bit for value and 1 bit for sign s0 maximum signed integer range is -2147483648 to 2147483647.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top