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 = 5和int y = -5可以在内存中具有相同的位表示吗?
5 个解决方案
#1
39
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. The actual values are given in limits.h
as INT_MIN
and INT_MAX
respectively.
int数据类型已签名,其最小范围至少为-32767到32767(含)。实际值在limits.h中给出,分别为INT_MIN和INT_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. Otherwise, there'd be no way to tell the difference.
并且,不管您使用哪种表示,5和-5都不能具有相同的编码。否则,就没有办法分辨出来。
#2
4
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.
C标准规定无符号数将以二进制形式存储。 (使用可选的填充位)。签名号码可以以三种格式之一存储:幅度和符号;两个补码或一个补码。有趣的是,它排除了某些其他表示,如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. 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通常为16或32位。标准规定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不是当前硬件迭代的最佳大小。
#3
3
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,则数字为正。“
#4
2
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位有符号整数时,它意味着它的一个位(最重要的)是一个标志,它将值标记为正或负。
#5
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将设置为unsigned int,则为十进制65531。
#1
39
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. The actual values are given in limits.h
as INT_MIN
and INT_MAX
respectively.
int数据类型已签名,其最小范围至少为-32767到32767(含)。实际值在limits.h中给出,分别为INT_MIN和INT_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. Otherwise, there'd be no way to tell the difference.
并且,不管您使用哪种表示,5和-5都不能具有相同的编码。否则,就没有办法分辨出来。
#2
4
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.
C标准规定无符号数将以二进制形式存储。 (使用可选的填充位)。签名号码可以以三种格式之一存储:幅度和符号;两个补码或一个补码。有趣的是,它排除了某些其他表示,如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. 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通常为16或32位。标准规定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不是当前硬件迭代的最佳大小。
#3
3
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,则数字为正。“
#4
2
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位有符号整数时,它意味着它的一个位(最重要的)是一个标志,它将值标记为正或负。
#5
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将设置为unsigned int,则为十进制65531。