24

I saw in MSDN documents that the maximum value of Int32 is 2,147,483,647, hexadecimal 0x7FFFFFFF.

I think, if it's Int32 it should store 32-bit integer values that finally should be 4,294,967,295 and hexadecimal 0xFFFFFFFF.

My question is why Int32 stores 31-bit integer values?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Afshin Mehrabani
  • 33,262
  • 29
  • 136
  • 201
  • Possible duplicate of [Why is the maximum value of an unsigned n-bit integer 2^n-1 and not 2^n?](https://stackoverflow.com/questions/5771520/why-is-the-maximum-value-of-an-unsigned-n-bit-integer-2n-1-and-not-2n) – phuclv Jul 21 '18 at 06:12

6 Answers6

37

It's because it's a signed integer. An unsigned 32-bit integer give you the value you expect.

Check out this MSDN page - http://msdn.microsoft.com/en-us/library/exx3b86w(v=vs.80).aspx

For a more in depth explanation on why this is check out the link in Jackson Popes answer related to Two's Complement number representation.

Also some further reading.

Lloyd
  • 29,197
  • 4
  • 84
  • 98
  • 2
    `int32` is a twos compliment number, so it's not strictly fair to say that the top bit is a sign bit. That would be a one's compliment number. – Servy Feb 25 '14 at 17:11
  • @Servy it is commonly called so, though. It's sort of misleading, but what can we do? – harold Feb 25 '14 at 17:16
  • 2
    @harold Not be misleading and make a correct statement instead. – Servy Feb 25 '14 at 17:22
  • @Servy but it *is* correct, just misleading. That bit *is* in fact called the sign bit, regardless of that naming being misleading. – harold Feb 25 '14 at 17:51
  • 1
    @harold So then have it not be misleading. – Servy Feb 25 '14 at 17:54
  • @Servy that sounds like a good plan, unfortunately it's impossible to execute. The bit has been given a misleading name, and we can neither change the name nor the misleadingness of it. – harold Feb 25 '14 at 17:58
  • 1
    @harold But you can omit the use of that term entirely, given that it's misleading. This is not even hard to do. – Servy Feb 25 '14 at 17:59
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48398/discussion-between-harold-and-servy) – harold Feb 25 '14 at 17:59
  • saying that one bit IS USED to store the sign is misleading. This "feature" comes naturally from the two's complement representation which is the actual internal representation for Int32 in C#. So no bit was actually "used" as sign bit, it just comes automatically as a good sideeffect of the two's complement representation. When I say "naturally" I mean that binary additions and subtractions works out of the box without any special code for the "sign bit". Please @JonSkeet, save us here!!! :-) – Gianluca Ghettini Sep 09 '15 at 10:44
  • 1
    I agree, when you subtract 2 from 1 you get -1, but in an Int thats -1 and the hex representation is not 0x80000001, but 0xFFFFFFFF ... that's what @Servy is trying to tell you. There is a big difference between using the MSB as a sign only and then having a 2-compliment number. The cool thing about 2-compliment is that you basically just offset the zero with half the size of max value and all math still works as expected with unsigned int's – BerggreenDK Sep 11 '15 at 08:38
9

Because one bit is used to store the sign (Int32 can be less than zero).

http://en.wikipedia.org/wiki/Two%27s_complement

Sulthan
  • 128,090
  • 22
  • 218
  • 270
Jackson Pope
  • 14,520
  • 6
  • 56
  • 80
7

Int32 and Int64 are both signed so they can handle integer values from -capacity/2 to (capacity/2)-1 (for zero) that is why the max value isn't the one you expected. But you can get what you want by using an unsigned int to have only positive numbers.

Servy
  • 202,030
  • 26
  • 332
  • 449
marcnc27
  • 146
  • 3
5

You are not considering the negative numbers. Int32 have the sign.

From MSDN: http://msdn.microsoft.com/en-us/library/system.int32.minvalue.aspx The MinValue is -2,147,483,648; that is, hexadecimal 0x80000000.

888
  • 3,246
  • 8
  • 41
  • 60
5

The first bit is the sign - an int32 is signed, i.e. it can be positive/negative (well I probably shouldn't say 'first' bit!)

Charleh
  • 13,749
  • 3
  • 37
  • 57
1

In a 2's complement signed n-bit type, the range is from -2n-1 to 2n-1-1 because with n bits you can represent 2n different values, half of which is used for signed numbers because of the sign bit. The remaining 2n-1 half is used for non-negative number. Since one is used for 0, there are only 2n-1-1 remaining values for positive numbers

phuclv
  • 37,963
  • 15
  • 156
  • 475