3

I am wondering why largest possible value of Int32 in .NET is 2147483647 but not 2147483648. Because 2³¹ = 2147483648.

Thank you

leppie
  • 115,091
  • 17
  • 196
  • 297
Vidya Bhatt
  • 59
  • 1
  • 1
  • 2
  • 8
    For the same reason that `sbyte` goes from -128 to 127. Think about that example for a while, because it's easier to consider small numbers. Think about how many different numbers are represented in the range `[-128, 127]`... – Jon Skeet Jul 18 '14 at 04:44
  • 1
    `2^31 = 10000000000000000000000000000000` which you cant use as the leading bit is for the sign. So one less (`11111111111111111111111111111111111`). – leppie Jul 18 '14 at 05:06
  • Thank you for the replies. Yeah.. same question again. Why sbyte's max value is 127? why not 128? 00000000 = 0 00000001 = 1 ... 01111111 = 128 right? – Vidya Bhatt Jul 18 '14 at 06:09
  • 3
    @VidyaBhatt 01111111 binary = 127 decimal. Hint: binary numbers where the lowest (right-most) bit is 1 can never be even, they must be odd. – Stefan Podskubka Sep 03 '14 at 10:41

4 Answers4

12

An Int32 is stored in 32 bits, not 31 bits, and half of its range is taken by negative numbers. Out of the remaining range, you lose one value to zero, leaving 2147483647 as the highest positive number.

The range for an Int32 is -2147483648 to 2147483647.

TheEvilPenguin
  • 5,634
  • 1
  • 26
  • 47
  • I understand that int is stored in 32 bits and left most bit is reserved for sign. What I don't understand is "Loose one value to zero".. how is that? because all zeros itself is 0. 000...0000 = 0 000...0001 = 1 000...0010 = 2 ... 011...1110 = 1073741824 011...1111 = 2147483648 – Vidya Bhatt Jul 18 '14 at 06:02
  • You should really spend some time on wikipedia learning about binary and signed integer representation. The representation commonly used for signed integers is called Two's Complement. Quickly, however, for an 8 bit number you can have `2^8`, or 256, values. We put exactly half of them (`2^7` or 128), on the negative side of zero, zero takes one, and we put the other half from zero up. As zero takes one, we're left with `2^7-1` positive numbers, or 127. This can be extended to 32-bit, where we get `2^31-1` positive numbers. Other number representations can also have -0 and +0. – TheEvilPenguin Jul 18 '14 at 09:15
7

It also includes zero 0 in the positive range. Hence the range is 0 to 2147483647 and since zero has been considered in the positive side hence towards negative side from '-1' to -2147483648.

So overall positive and negative side takes equal number of values.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
6

The actual data of Int32 is stored in 31 bits, because regular Int32 should also hold negative numbers, 1 bit is used as a sign bit, 31 bits that remain are used as data. However, if you use unsigned Int32, then you will have a complete 32 bits of data in it.

Csharptor
  • 61
  • 1
  • 1
1

It mean int can have maximum 2147483648 positive values starting from 0 to 2147483647, The int.Min is -2,147,483,648 as it does not include the 0

Adil
  • 146,340
  • 25
  • 209
  • 204