C中unsigned int和signed int有什么区别?
考虑这些定义:
int x=5;
int y=-5;
unsigned int z=5;
它们如何存储在内存中?有人能解释一下这些在内存中的位表示吗?
int x=5
和 int y=-5
在内存中可以有相同的位表示吗?
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=5
and int y=-5
have same bit representation in memory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设 int 是一个 16 位整数(这取决于 C 实现,现在大多数是 32 位),位表示有所不同,如下所示:
如果将二进制 1111111111111011 设置为无符号 int,则它将是十进制 65531。
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:
if binary 1111111111111011 would be set to an unsigned int, it would be decimal 65531.
这是一个非常好的链接,解释了 C 中带符号和无符号 INT 的存储 -
http:// /answers.yahoo.com/question/index?qid=20090516032239AAzcX1O
摘自上述文章 -
“称为二进制补码的过程用于将正数转换为负数。这样做的副作用是最高有效位用于告诉计算机该数是正数还是负数,如果最高有效位为 1,则该数为负数。”
Here is the very nice link which explains the storage of signed and unsigned INT in C -
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."
C 标准指定无符号数将以二进制形式存储。 (带有可选的填充位)。有符号数可以以三种格式之一存储: 大小和符号;二进制补码或一个补码。有趣的是,这排除了某些其他表示,例如 Excess-n 或 Base -2。
然而,在大多数机器和编译器上,以 2 的补码形式存储带符号的数字。
int
通常为 16 或 32 位。该标准规定int
应该是对底层处理器最有效的,只要它是>= Short
和<= long
> 那么这是标准允许的。然而,在某些机器和操作系统上,历史导致
int
并不是当前硬件迭代的最佳大小。The C standard specifies 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.
However on most machines and compilers store signed numbers in 2's complement.
int
is normally 16 or 32 bits. The standard says thatint
should be whatever is most efficient for the underlying processor, as long as it is>= short
and<= long
then it is allowed by the standard.On some machines and OSs history has causes
int
not to be the best size for the current iteration of hardware however.因为这一切都与内存有关,所以最终所有数值都以二进制形式存储。
32 位无符号整数可以包含从所有二进制 0 到所有二进制 1 的值。
当涉及到 32 位有符号整数时,这意味着它的一位(最高有效位)是一个标志,它标记该值是正数还是负数。
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.
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.
ISO C 说明了差异所在。
int
数据类型是有符号的,并且最小范围至少为 -32767 到 32767(含)。实际值在limits.h
中分别给出为INT_MIN
和INT_MAX
。unsigned int
的最小范围为 0 到 65535(含),实际最大值为同一头文件中的UINT_MAX
。除此之外,该标准不强制要求使用二进制补码表示法来编码值,这只是可能性之一。三种允许的类型将具有以下 5 和 -5 的编码(使用 16 位数据类型):
请注意,正值对于所有表示都具有相同的编码,只有负值不同。
进一步注意,对于无符号值,您不需要使用其中一位作为符号。这意味着您可以在积极方面获得更多范围(当然,代价是没有消极编码)。
不,无论您使用哪种表示形式,
5
和-5
都不能具有相同的编码。否则的话,就没有办法区分。顺便说一句,目前 C 和 C++ 标准都在采取行动,指定二进制补码作为负整数的唯一编码。
ISO C states what the differences are.
The
int
data type is signed and has a minimum range of at least -32767 through 32767 inclusive. The actual values are given inlimits.h
asINT_MIN
andINT_MAX
respectively.An
unsigned int
has a minimal range of 0 through 65535 inclusive with the actual maximum value beingUINT_MAX
from that same header file.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):
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,
5
and-5
cannot have the same encoding regardless of which representation you use. Otherwise, there'd be no way to tell the difference.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.