C# 为什么 Int32 的最大值是 0x7FFFFFFF?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13230041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:49:45  来源:igfitidea点击:

Why is Int32's maximum value 0x7FFFFFFF?

c#typesint32

提问by Afshin Mehrabani

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

我在MSDN文档中看到的最大值Int322,147,483,647,十六进制0x7FFFFFFF

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

我认为,如果它Int32应该存储 32 位整数值,最终应该是4,294,967,295和 hexadecimal 0xFFFFFFFF

My question is why Int32stores 31-bit integer values?

我的问题是为什么要Int32存储 31 位整数值?

采纳答案by Lloyd

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

这是因为它是一个有符号整数。一个无符号的 32 位整数给你你期望的值。

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

查看此 MSDN 页面 - 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 Hymanson Popes answer related to Two's Complement number representation.

有关原因的更深入解释,请查看 Hymanson Popes 与 Two's Complement number representation 相关的答案中的链接。

Also some further reading.

还有一些进一步的阅读

回答by Hymanson Pope

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

因为一位用于存储符号(Int32 可以小于零)。

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

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

回答by 888

You are not considering the negative numbers. Int32have the sign.

你没有考虑负数。 Int32有标志。

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

从MSDN:http://msdn.microsoft.com/en-us/library/system.int32.minvalue.aspxMinValue-2,147,483,648; 即十六进制0x80000000

回答by Charleh

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!)

第一位是符号 - 一个 int32 是有符号的,即它可以是正/负(好吧,我可能不应该说“第一个”位!)

回答by marcnc27

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.

Int32 和 Int64 都是有符号的,因此它们可以处理从 -capacity/2 到 (capacity/2)-1(为零)的整数值,这就是最大值不是您期望的值的原因。但是您可以通过使用 unsigned int 只有正数来获得您想要的东西。

回答by phuclv

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

在 2 的有符号 n 位补码类型中,范围是从 -2 n-1到 2 n-1-1 因为使用 n 位您可以表示 2 n 个不同的值,由于符号,其中一半用于有符号数少量。剩下的 2 n-1一半用于非负数。由于 1 用于 0,因此只有 2 n-1-1 个剩余值可用于正数