INT max size for 32bit system
Asked Answered
S

7

1

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?

Seabolt answered 7/1, 2012 at 16:5 Comment(0)
D
4

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.

Dubbing answered 7/1, 2012 at 16:14 Comment(0)
B
1

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.

Bodiless answered 7/1, 2012 at 16:9 Comment(0)
C
1

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."

Comose answered 7/1, 2012 at 16:11 Comment(0)
E
1

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.

Extraversion answered 7/1, 2012 at 16:12 Comment(1)
The largest float value is dependent on the platform, but is certainly more than 10,000,000,000,000 (1e13). The PHP on my systems seems to be limited to 1.7976e308.Revolution
F
0

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.

Fokos answered 7/1, 2012 at 16:8 Comment(0)
B
0

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.

Burdock answered 7/1, 2012 at 16:9 Comment(0)
M
0

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.

Morpho answered 7/1, 2012 at 16:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.