INT max size for 32bit system

醉酒当歌 提交于 2019-12-02 08:46:22

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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




回答7:


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.



来源:https://stackoverflow.com/questions/8770992/int-max-size-for-32bit-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!