Search code examples
phpint32-bitunsigned-integer

INT max size for 32bit system


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?


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.