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

发布于 2024-09-25 02:12:10 字数 186 浏览 3 评论 0原文

考虑这些定义:

int x=5;
int y=-5;
unsigned int z=5;

它们如何存储在内存中?有人能解释一下这些在内存中的位表示吗?

int x=5int 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

厌倦 2024-10-02 02:12:36

假设 int 是一个 16 位整数(这取决于 C 实现,现在大多数是 32 位),位表示有所不同,如下所示:

 5 = 0000000000000101
-5 = 1111111111111011

如果将二进制 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:

 5 = 0000000000000101
-5 = 1111111111111011

if binary 1111111111111011 would be set to an unsigned int, it would be decimal 65531.

留一抹残留的笑 2024-10-02 02:12:27

这是一个非常好的链接,解释了 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."

把梦留给海 2024-10-02 02:12:25

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 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.

On some machines and OSs history has causes int not to be the best size for the current iteration of hardware however.

梦醒灬来后我 2024-10-02 02:12:22

因为这一切都与内存有关,所以最终所有数值都以二进制形式存储。

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.

奢望 2024-10-02 02:12:19

ISO C 说明了差异所在。

int 数据类型是有符号的,并且最小范围至少为 -32767 到 32767(含)。实际值在 limits.h 中分别给出为 INT_MININT_MAX

unsigned int 的最小范围为 0 到 65535(含),实际最大值为同一头文件中的 UINT_MAX

除此之外,该标准不强制要求使用二进制补码表示法来编码值,这只是可能性之一。三种允许的类型将具有以下 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 |
    +---------------------+---------------------+---------------------+
  • 在补码中,通过反转所有位然后加 1 可以得到负数。
  • 在补码中,可以得到通过反转所有位来求负数。
  • 在符号/幅度中,最高位是符号,因此只需将其反转即可得到负数。

请注意,正值对于所有表示都具有相同的编码,只有负值不同。

进一步注意,对于无符号值,您不需要使用其中一位作为符号。这意味着您可以在积极方面获得更多范围(当然,代价是没有消极编码)。

不,无论您使用哪种表示形式,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 in limits.h as INT_MIN and INT_MAX respectively.

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.

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):

        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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文