C语言 C中的unsigned int和signed int有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3812022/
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
What is a difference between unsigned int and signed int in C?
提问by Anand Kumar
Consider these definitions:
考虑这些定义:
int x=5;
int y=-5;
unsigned int z=5;
How are they stored in memory? Can anybody explain the bit representation of these in memory?
它们是如何存储在内存中的?任何人都可以解释这些在内存中的位表示吗?
Can int x=5and int y=-5have same bit representation in memory?
可以int x=5并且int y=-5在内存中具有相同的位表示吗?
回答by paxdiablo
ISO C states what the differences are.
ISO C 说明了不同之处。
The intdata type is signed and has a minimum range of at least -32767 through 32767 inclusive. The actual values are given in limits.has INT_MINand INT_MAXrespectively.
的int数据类型是有符号的,并且具有至少-32767的最小范围到32767以下。实际值分别在limits.hasINT_MIN和中给出INT_MAX。
An unsigned inthas a minimal range of 0 through 65535 inclusive with the actual maximum value being UINT_MAXfrom that same header file.
Anunsigned int的最小范围为 0 到 65535,实际最大值UINT_MAX来自同一个头文件。
Beyond that, the standard does not mandate twos complement notation for encoding the values, that's just one of the possibilities. The three allowed types would have encodings of the following for 5 and -5 (using 16-bit data types):
除此之外,该标准不强制要求使用二进制补码符号对值进行编码,这只是其中一种可能性。三种允许的类型将具有以下 5 和 -5 的编码(使用 16 位数据类型):
two's complement | ones' complement | sign/magnitude
+---------------------+---------------------+---------------------+
5 | 0000 0000 0000 0101 | 0000 0000 0000 0101 | 0000 0000 0000 0101 |
-5 | 1111 1111 1111 1011 | 1111 1111 1111 1010 | 1000 0000 0000 0101 |
+---------------------+---------------------+---------------------+
- In two's complement, you get a negative of a number by inverting all bits then adding 1.
- In ones' complement, you get a negative of a number by inverting all bits.
- In sign/magnitude, the top bit is the sign so you just invert that to get the negative.
- 在二进制补码中,您通过反转所有位然后加 1 得到一个负数。
- 在一个的补码中,您通过反转所有位得到一个数字的负数。
- 在符号/幅度中,最高位是符号,因此您只需将其反转即可获得负数。
Note that positive values have the same encoding for all representations, only the negative values are different.
请注意,正值对所有表示具有相同的编码,只有负值不同。
Note further that, for unsigned values, you do not need to use one of the bits for a sign. That means you get more range on the positive side (at the cost of no negative encodings, of course).
进一步注意,对于无符号值,您不需要使用其中一位作为符号。这意味着您在积极方面获得了更多范围(当然,以没有消极编码为代价)。
And no, 5and -5cannot have the same encoding regardless of which representation you use. Otherwise, there'd be no way to tell the difference.
不,5并且-5无论您使用哪种表示都不能具有相同的编码。否则,就没有办法区分。
As an aside, there are currently moves underway, in both C and C++ standards, to nominate two's complement as the only encoding for negative integers.
顺便说一句,目前在 C 和 C++ 标准中都在采取行动,将二进制补码指定为负整数的唯一编码。
回答by Prashant
Because it's all just about memory, in the end all the numerical values are stored in binary.
因为这一切都与内存有关,所以最终所有的数值都以二进制形式存储。
A 32 bit unsigned integer can contain values from all binary 0s to all binary 1s.
一个 32 位无符号整数可以包含从所有二进制 0 到所有二进制 1 的值。
When it comes to 32 bit signed integer, it means one of its bits (most significant) is a flag, which marks the value to be positive or negative.
当涉及到 32 位有符号整数时,这意味着它的一个位(最重要的)是一个标志,它将值标记为正数或负数。
回答by Douglas Leeder
The C standardspecifies that unsigned numbers will be stored in binary. (With optional padding bits). Signed numbers can be stored in one of three formats: Magnitude and sign; two's complement or one's complement. Interestingly that rules out certain other representations like Excess-n or Base ?2.
在C标准指定的是无符号数将被存储在二进制文件。(带有可选的填充位)。带符号的数字可以以三种格式之一存储:幅度和符号;补码或补码。有趣的是,这排除了某些其他表示,如Excess-n 或 Base ?2。
However on most machines and compilers store signed numbers in 2's complement.
然而,在大多数机器和编译器上,以 2 的补码形式存储有符号数。
intis normally 16 or 32 bits. The standard says that intshould be whatever is most efficient for the underlying processor, as long as it is >= shortand <= longthen it is allowed by the standard.
int通常为 16 位或 32 位。标准说,这 int应该是对底层处理器最有效的任何方式,只要它是>= short并且<= long标准允许的。
On some machines and OSs history has causes intnot to be the best size for the current iteration of hardware however.
然而,在某些机器和操作系统上,历史原因int并不是当前硬件迭代的最佳尺寸。
回答by Sachin Shanbhag
Here is the very nice link which explains the storage of signed and unsigned INT in C -
这是一个很好的链接,它解释了在 C 中存储有符号和无符号 INT -
http://answers.yahoo.com/question/index?qid=20090516032239AAzcX1O
http://answers.yahoo.com/question/index?qid=20090516032239AAzcX1O
Taken from this above article -
取自上述文章-
"process called two's complement is used to transform positive numbers into negative numbers. The side effect of this is that the most significant bit is used to tell the computer if the number is positive or negative. If the most significant bit is a 1, then the number is negative. If it's 0, the number is positive."
“使用称为二进制补码的过程将正数转换为负数。这样做的副作用是最高有效位用于告诉计算机该数字是正数还是负数。如果最高有效位是 1,则数字为负数。如果为 0,则数字为正数。”
回答by elsni
Assuming int is a 16 bit integer (which depends on the C implementation, most are 32 bit nowadays) the bit representation differs like the following:
假设 int 是一个 16 位整数(这取决于 C 实现,现在大多数是 32 位)位表示不同,如下所示:
5 = 0000000000000101
-5 = 1111111111111011
if binary 1111111111111011 would be set to an unsigned int, it would be decimal 65531.
如果将二进制 1111111111111011 设置为无符号整数,则将是十进制 65531。

