C语言 为什么 int 的范围是 -32768 到 32767?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18558271/
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
Why the range of int is -32768 to 32767?
提问by VJain
Why the range of any data type is greater on negative side as compare to positive side?
为什么任何数据类型的范围在消极方面都比积极方面更大?
For example, in case of integer:
例如,在整数的情况下:
In Turbo Cits range is -32768to 32767and for Visual Studioit is -2147483648to 2147483647.
在Turbo C的它的范围是-32768要32767和Visual Studio的是-2147483648要2147483647。
The same happens to other data types...
其他数据类型也会发生同样的情况......
[UPD: Set proper limit values for Visual Studio]
[UPD:为Visual Studio设置适当的限制值]
回答by aaaaaa123456789
Because of how numbers are stored. Signed numbers are stored using something called "two's complement notation".
因为数字是如何存储的。有符号数使用称为“二进制补码表示法”的东西存储。
Remember all variables have a certain amount of bits. If the most significant one of them, the one on the left, is a 0, then the number is non-negative (i.e., positive or zero), and the rest of the bits simply represent the value.
记住所有变量都有一定数量的位。如果其中最重要的一位,即左边的一位是 0,则该数字为非负数(即正数或零),其余位仅表示该值。
However, if the leftmost bit is a 1, then the number is negative. The real value of the number can be obtained by subtracting 2^n from the whole number represented (as an unsigned quantity, including the leftmost 1), where n is the amount of bits the variable has.
但是,如果最左边的位是 1,则该数字为负数。可以通过从表示的整数(作为无符号量,包括最左边的 1)中减去 2^n 来获得数字的实际值,其中 n 是变量具有的位数。
Since only n - 1 bits are left for the actual value (the "mantissa") of the number, the possible combinations are 2^(n - 1). For positive/zero numbers, this is easy: they go from 0, to 2^(n - 1) - 1. That -1 is to account for zero itself -- for instance, if you only had four possible combinations, those combinations would represent 0, 1, 2, and 3 (notice how there's four numbers): it goes from 0 to 4 - 1.
由于数字的实际值(“尾数”)只剩下 n - 1 位,因此可能的组合是 2^(n - 1)。对于正数/零数,这很容易:它们从 0 到 2^(n - 1) - 1。那个 -1 是为了说明零本身——例如,如果你只有四种可能的组合,这些组合将代表 0、1、2 和 3(注意有四个数字):它从 0 到 4 - 1。
For negative numbers, remember the leftmost bit is 1, so the whole number represented goes between 2^(n - 1) and (2^n) - 1 (parentheses are very important there!). However, as I said, you have to take 2^n away to get the real value of the number. 2^(n - 1) - 2^n is -(2^(n - 1)), and ((2^n) - 1) - 2^n is -1. Therefore, the negative numbers' range is -(2^(n - 1)) to -1.
对于负数,记住最左边的位是 1,所以表示的整数在 2^(n - 1) 和 (2^n) - 1 之间(括号在那里非常重要!)。但是,正如我所说,您必须减去 2^n 才能获得数字的真实值。2^(n - 1) - 2^n 是 -(2^(n - 1)),而 ((2^n) - 1) - 2^n 是 -1。因此,负数的范围是 -(2^(n - 1)) 到 -1。
Put all that together and you get -2^(n - 1) to 2^(n - 1) - 1. As you can see, the upper bound gets a -1 that the lower bound doesn't.
把所有这些放在一起,你会得到 -2^(n - 1) 到 2^(n - 1) - 1。正如你所看到的,上界得到一个 -1 而下界没有。
And that's why there's one more negative number than positive.
这就是为什么负数比正数多一个的原因。
回答by paxdiablo
The minimum range required by C is actually -32767 through 32767, because it has to cater for two's complement, ones' complement and sign/magnitude encoding for negative numbers, all of which the C standard allows. See Annex E, Implementation limitsof C11 (and C99) for details on the minimum ranges for data types.
C 所需的最小范围实际上是 -32767 到 32767,因为它必须满足 C 标准允许的二进制补码、一补码和负数的符号/幅度编码。有关Annex E, Implementation limits数据类型的最小范围的详细信息,请参阅C11(和 C99)。
Your question pertains only to the two's complement variant and the reason for that is simple. With 16 bits, you can represent 216(or 65,536) different values and zero has to be one of those. Hence there are an odd number of values left, of which the majority (by one) are negative values:
您的问题仅与两个补码变体有关,原因很简单。使用 16 位,您可以表示 2 16(或 65,536)个不同的值,零必须是其中之一。因此,剩下奇数个值,其中大多数(1 个)是负值:
1 thru 32767 = 37267 values
0 = 1 value
-1 thru -32768 = 32768 values
-----
65536 values
Both ones' complement and sign-magnitude encoding allow for a negative-zero value (as well as positive-zero), meaning that one less bit pattern is available for the non-zero numbers, hence the reduced minimum range you find in the standard.
补码和符号幅度编码都允许负零值(以及正零),这意味着非零数字可用的位模式少了一个,因此您在标准中找到的最小范围减小了.
1 thru 32767 = 37267 values
0 = 1 value
-0 = 1 value
-1 thru -32767 = 32767 values
-----
65536 values
Two's complement is actually a nifty encoding scheme because positive and negative numbers can be added together with the same simple hardware. Other encoding schemes tend to require more elaborate hardware to do the same task.
二进制补码实际上是一种很好的编码方案,因为可以使用相同的简单硬件将正数和负数相加。其他编码方案往往需要更复杂的硬件来完成相同的任务。
For a fuller explanation on how two's complement works, see the wikipedia page.
有关二进制补码如何工作的更完整说明,请参阅维基百科页面。
回答by Miles Rout
Because the range includes zero. The number of different values an n-bit integer can represent is 2^n. That means a 16-bit integer can represent 65536 different values. If it's an unsigned 16-bit integer, it can represent 0-65535 (inclusive). The convention for signed integers is to represent -32768 to 32767, -214748368 to 214748367, etc.
因为范围包括零。一个 n 位整数可以表示的不同值的数量是 2^n。这意味着一个 16 位整数可以表示 65536 个不同的值。如果是无符号的16位整数,则可以表示0-65535(含)。有符号整数的约定是表示 -32768 到 32767、-214748368 到 214748367 等。
回答by Andon M. Coleman
Ordinarily, due to using a two's complement system for storing negative values, when you flip the sign bit on an integer it's biased toward the negative.
通常,由于使用二进制补码系统存储负值,当您翻转整数的符号位时,它会偏向负数。
The range should be: -(2^(n-1)) - ((2^(n-1)-1)
范围应该是:-(2^(n-1)) - ((2^(n-1)-1)
回答by Steve Barnes
With 2s complement negative numbers are defined as the bitwise not plus 1 this reduces the range of possible numbers in a given number of bits by 1 on the negative side.
使用 2s 补码负数定义为按位不加 1,这将给定位数中可能的数字范围在负侧减少 1。

