简体   繁体   English

C中的unsigned int和signed int有什么区别?

[英]What is a difference between unsigned int and signed int in C?

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? int x=5int y=-5可以在内存中具有相同的位表示吗?

ISO C states what the differences are. ISO C 说明了不同之处。

The int data type is signed and has a minimum range of at least -32767 through 32767 inclusive. int数据类型是有符号的,其最小范围至少为 -32767 到 32767(含)。 The actual values are given in limits.h as INT_MIN and INT_MAX respectively.实际值在limits.h分别给出为INT_MININT_MAX

An unsigned int has a minimal range of 0 through 65535 inclusive with the actual maximum value being UINT_MAX from that same header file. unsigned 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.在二进制补码中,您通过反转所有位然后加 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.在符号/幅度中,最高位是符号,因此您只需将其反转即可获得负数。

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.不,无论您使用哪种表示, 5-5都不能具有相同的编码。 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.顺便说一句,目前在 C 和 C++ 标准中都在采取行动,将二进制补码指定为负整数的唯一编码。

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 位有符号整数时,这意味着它的一个位(最重要的)是一个标志,它将值标记为正数或负数。

The C standard specifies that unsigned numbers will be stored in binary. C 标准规定无符号数将以二进制形式存储。 (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 .有趣的是,这排除了某些其他表示,如Excess-n 或 Base −2

However on most machines and compilers store signed numbers in 2's complement.然而,在大多数机器和编译器上,以 2 的补码形式存储有符号数。

int is normally 16 or 32 bits. int通常是 16 位或 32 位。 The standard says that int 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.标准说int应该是对底层处理器最有效的任何东西,只要它是>= short<= long那么它是标准所允许的。

On some machines and OSs history has causes int not to be the best size for the current iteration of hardware however.然而,在某些机器和操作系统上,历史导致int不是当前硬件迭代的最佳大小。

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,则数字为正数。”

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。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM