Why is Int32's maximum value 0x7FFFFFFF?

23

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?

c#
types
int32
asked on Stack Overflow Nov 5, 2012 by Afshin Mehrabani • edited May 21, 2019 by phuclv

6 Answers

34

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.

answered on Stack Overflow Nov 5, 2012 by Lloyd • edited Jun 27, 2017 by Lloyd
8

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

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

answered on Stack Overflow Nov 5, 2012 by Jackson Pope • edited Nov 5, 2012 by Sulthan
6

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.

answered on Stack Overflow Nov 5, 2012 by marcnc27 • edited Feb 25, 2014 by Servy
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.

answered on Stack Overflow Nov 5, 2012 by 888 • edited Dec 7, 2012 by 888
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!)

answered on Stack Overflow Nov 5, 2012 by Charleh
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

answered on Stack Overflow May 30, 2014 by phuclv

User contributions licensed under CC BY-SA 3.0