本文介绍了为什么无符号整数0xFFFFFFFF的是等于为int -1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C或C ++据说一个为size_t(无符号int数据类型)可以容纳的最大数量是一样的铸件-1至该数据类型。例如参见无效值

In C or C++ it is said that the maximum number a size_t (an unsigned int data type) can hold is the same as casting -1 to that data type. for example see Invalid Value for size_t

为什么呢?

我的意思是,(谈到32位整数)AFAIK最显著位保持在一个签名的数据类型的标志(即,位0x80000000的,以形成一个负数)。那么,1是00000001 .. 0x7FFFFFFFF是最大的正数一个int数据类型可以持有。

I mean, (talking about 32 bit ints) AFAIK the most significant bit holds the sign in a signed data type (that is, bit 0x80000000 to form a negative number). then, 1 is 0x00000001.. 0x7FFFFFFFF is the greatest positive number a int data type can hold.

然后,AFAIK -1 INT的二进制重新presentation应0x80000001(也许是我错了)。为什么/这个二进制值如何转换为任何事情完全不同(0xFFFFFFFF的)铸造整数为unsigned时?或..怎么可能形成一个二进制-1出0xFFFFFFFF的吗?

Then, AFAIK the binary representation of -1 int should be 0x80000001 (perhaps I'm wrong). why/how this binary value is converted to anything completely different (0xFFFFFFFF) when casting ints to unsigned?? or.. how is it possible to form a binary -1 out of 0xFFFFFFFF?

我毫不怀疑,在C:((无符号整数)-1)== 0xFFFFFFFF的或((int)的0xFFFFFFFF的)== -1是同样真实比1 + 1 == 2,我只是想知道为什么

I have no doubt that in C: ((unsigned int)-1) == 0xFFFFFFFF or ((int)0xFFFFFFFF) == -1 is equally true than 1 + 1 == 2, I'm just wondering why.

推荐答案

C和C ++可以在许多不同的架构上运行,并且机器类型。因此,他们可以有数字的不同重新presentations:2的补码和一补数是最常见的。一般来说,你不应该依赖于程序中的一个特定的再presentation。

C and C++ can run on many different architectures, and machine types. Consequently, they can have different representations of numbers: Two's complement, and Ones' complement being the most common. In general you should not rely on a particular representation in your program.

有关无符号整型(为size_t 是其中之一),C标准(和C ++标准也是如此,我认为)指定precise溢出规则。总之,如果 SIZE_MAX 的类型为size_t 的最大值,那么前pression

For unsigned integer types (size_t being one of those), the C standard (and the C++ standard too, I think) specifies precise overflow rules. In short, if SIZE_MAX is the maximum value of the type size_t, then the expression

(为size_t)(SIZE_MAX + 1)

保证是 0 ,因此,你可以肯定的是(为size_t)-1 等于到 SIZE_MAX 。这同样适用于其他无符号类型正确的。

is guaranteed to be 0, and therefore, you can be sure that (size_t) -1 is equal to SIZE_MAX. The same holds true for other unsigned types.

注意上面也是如此:


  • 所有的无符号类型,

  • 即使底层机器没有二进制补码重新present号的。在这种情况下,编译器必须确保身份成立。

  • for all unsigned types,
  • even if the underlying machine doesn't represent numbers in Two's complement. In this case, the compiler has to make sure the identity holds true.

此外,上述意味着你不能依赖于特定的重presentations为的签署的类型。

Also, the above means that you can't rely on specific representations for signed types.

修改的:为了回答一些评论的:

Edit: In order to answer some of the comments:

让我们说我们有一个code片断如下:

Let's say we have a code snippet like:

int i = -1;
long j = i;

有在赋值类型转换为Ĵ。假设 INT 有不同的大小(最[所有?] 64位系统),比特模式在为 I j存储地点将是不同的,因为他们有不同的尺寸。编译器可以确保的的我Ĵ 1

There is a type conversion in the assignment to j. Assuming that int and long have different sizes (most [all?] 64-bit systems), the bit-patterns at memory locations for i and j are going to be different, because they have different sizes. The compiler makes sure that the values of i and j are -1.

同样,当我们做的:

size_t s = (size_t) -1

有一个类型转换正在进行。在 1 的类型为 INT 。它有一个位模式,但是这是不相关的这个例子,因为当转换到为size_t 发生因投,编译器会翻译的的根据(在这种情况为size_t )的类型的规则。因此,即使 INT 为size_t 有不同的尺寸,标准保证存储在取值上方将是最大值为size_t 可以。

There is a type conversion going on. The -1 is of type int. It has a bit-pattern, but that is irrelevant for this example because when the conversion to size_t takes place due to the cast, the compiler will translate the value according to the rules for the type (size_t in this case). Thus, even if int and size_t have different sizes, the standard guarantees that the value stored in s above will be the maximum value that size_t can take.

如果我们这样做:

long j = LONG_MAX;
int i = j;

如果 LONG_MAX 大于 INT_MAX ,然后在值 I 是实现定义(C89,节3.2.1.2)。

If LONG_MAX is greater than INT_MAX, then the value in i is implementation-defined (C89, section 3.2.1.2).

这篇关于为什么无符号整数0xFFFFFFFF的是等于为int -1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 17:38