-2

We know that an int value has a maximum value of 2^31 - 1 and a minimum value of -2^31. If we were to set int to the maximum value:

int x = int.MaxValue;

And we take that x and add one to it in a unchecked field

unchecked
{
    x++;
}

Then we get x = the minimum value of int. My question is why does this happen and how does it happen in terms of binary.

dxdydsdz
  • 15
  • 2

3 Answers3

5

In C#, the built-in integers are represented by a sequence of bit values of a predefined length. For the basic int datatype that length is 32 bits. Since 32 bits can only represent 4,294,967,296 different possible values (since that is 2^32),

Since int can hold both positive and negative numbers, the sign of the number must be encoded somehow. This is done with first bit. If the first bit is 1, then the number is negative.

Here are the int values laid out on a number-line in hexadecimal and decimal:

 Hexadecimal        Decimal
 -----------    -----------
 0x80000000     -2147483648
 0x80000001     -2147483647
 0x80000002     -2147483646
    ...              ...
 0xFFFFFFFE              -2
 0xFFFFFFFF              -1
 0x00000000               0
 0x00000001               1
 0x00000002               2
     ...             ...
 0x7FFFFFFE      2147483646
 0x7FFFFFFF      2147483647

As you can see from this chart, the bits that represent the smallest possible value are what you would get by adding one to the largest possible value, while ignoring the interpretation of the sign bit. When a signed number is added in this way, it is called "integer overflow". Whether or not an integer overflow is allowed or treated as an error is configurable with the checked and unchecked statements in C#.

This representation is called 2's complement

You can check this link if you want to go deeper.

Frebin Francis
  • 1,905
  • 1
  • 11
  • 19
-2

Int has a maximum value of 2^31 - 1 because , int is an alias for Int32 that stores 4 bytes in memory to represent the value.

Now lets come to the concept of int.Max + 1. Here you need to know about the Signed number representations that is used to store the negative values. In Binary number representation, there's nothing like negative numbers but they are represented by the one's complement and two's complement bits.

Lets say, my int1 has a storage memory of 1 byte, ie. 8 bits. So Max value you can store into int1 is 2^8 -1 = 255 . Now let's add 1 into this value-

     11111111 
   + 00000000 
   ----------
    100000000

your output is 100000000 = (512) in decimal, that is beyond the storage capacity of int1 and that represents the negative value of -256 in decimal (as first bit shows the negative value).

This is the reason adding 1 to int.Max become, int.Minimum. ie.

 `int.Maximum + 1 = int.Minimum`
Rohit Prakash
  • 1,975
  • 1
  • 14
  • 24
-2

Okay, I'm going to assume for sake of convenience that there's a 4 bit integer type in C#. The most significant bit is used to store the sign, the remaining three are used to store the magnitude.

The max number that can be stored in such a representation is +7 (positive 7 in the base 10). In binary, this is:

0111

Now let's add positive one:

 0111
+0001
_____
 1000

Whoops, that carried over from the magnitudes and flipped the sign bit! The sum you see above is actually the number -8, the smallest possible number that can be stored in this representation. If you're not familiar with two's complement, which is how signed numbers are commonly represented in binary, you might think that number is negative zero (which would be the case in a sign and magnitude representation). You can read up on two's complement to understand this better.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139