简体   繁体   English

UINT_MAX的所有位都设置为1吗?

[英]Does UINT_MAX have all bits set to 1?

This question is asked before but I am still confused. 这个问题之前被问过,但我仍然感到困惑。

I know that 我知道

unsigned int a = -1;

would be UINT_MAX . 将是UINT_MAX But it is not because all bits of -1 is set. 但这并不是因为-1的所有位都已设置。 C11 says C11说

if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type 如果新类型是无符号的,则通过重复加或减一个可以在新类型中表示的最大值来转换该值,直到该值在新类型的范围内为止

So lets say UINT_MAX is 100 (I know it should be greater then 2^16-1 but lets ignore this for now) 所以我们说UINT_MAX是100(我知道它应该大于2 ^ 16-1但现在让我们忽略它)

unsigned int a = -1; // will be
unsigned int a = -1 + UINT_MAX + 1; // 100 = UINT_MAX  

Standard only says UINT_MAX >= 2^16-1 . 标准仅说UINT_MAX >= 2^16-1 But does it say anywhere it should be 2^n-1? 但它是否应该说它应该是2 ^ n-1?

Also is answer different in C++? 在C ++中答案也不同?

In C, the maximum value for an unsigned integer must be in the form 1 : 2 N - 1. 在C中,无符号整数的最大值必须采用1 :2 N - 1的形式。

Thus all value bits of the value UINT_MAX will be set to 1. There may be padding bits, whose values are unspecified. 因此,值UINT_MAX的所有位将被设置为1.可能存在填充位,其值未指定。


1 (Quoted from: ISO/IEC 9899:201x 6.2.6.2 Integer types 1) 1 (引用自:ISO / IEC 9899:201x 6.2.6.2整数类型1)
For unsigned integer types other than unsigned char, the bits of the object representation shall be divided into two groups: value bits and padding bits (there need not be any of the latter). 对于unsigned char以外的无符号整数类型,对象表示的位应分为两组:值位和填充位(不需要后者中的任何一个)。 If there are N value bits, each bit shall represent a different power of 2 between 1 and 2 N −1 , so that objects of that type shall be capable of representing values from 0 to 2 N −1 using a pure binary representation; 如果有N个值位,则每个位应表示1和2 N -1之间的2的不同幂,因此该类型的对象应能够使用纯二进制表示来表示0到2 N -1的值; this shall be known as the value representation. 这应该被称为价值表示。 The values of any padding bits are unspecified. 任何填充位的值都未指定。

No, not quite. 不,不完全。

An unsigned type can consist of value bits and padding bits . 无符号类型可以由值位填充位组成

You are correct that the value bits will always be set to 1 for the maximum value, but the specific values of the padding bits is left to the implementation. 您是正确的,最大值的值位始终设置为1,但填充位的具体值留给实现。 So this means that UINT_MAX needs to be a Mersenne number . 所以这意味着UINT_MAX需要是一个Mersenne数 Other requirements state it can't be less than 65535. 其他要求规定它不能低于65535。

C and C++ are equivalent in this respect. 在这方面,C和C ++是等价的。

You're correct to say that by the definition of conversions, -1 converted to unsigned int is guaranteed to be UINT_MAX. 你说通过转换的定义,转换为unsigned int的-1保证是UINT_MAX是正确的。 It hasn't anything to do with any bit patterns. 它与任何位模式无关。 If there was an implementation where UINT_MAX was 100, then -1 converted to unsigned int would be 100. 如果存在UINT_MAX为100的实现,则-1转换为unsigned int将为100。

There are reasons why UINT_MAX cannot be 100: One because it must be ≥ 2^16-1, but that would allow UINT_MAX = 1,000,000. UINT_MAX不能为100的原因有一个:一,因为它必须≥2^ 16-1,但这将允许UINT_MAX = 1,000,000。 Second, because unsigned int must have a binary representation with some fixed number n of value bits, so UINT_MAX = 2^n - 1. 其次,因为unsigned int必须具有一个具有固定数量n的值位的二进制表示,所以UINT_MAX = 2 ^ n - 1。

It is possible that INT_MAX = 2^31 - 1 and UINT_MAX = 2^31 - 1 (not 2^32 - 1 as it is usually). INT_MAX = 2 ^ 31-1和UINT_MAX = 2 ^ 31-1(通常不是2 ^ 32-1) 可能的。 In that case -1 would have 32 bits set; 在这种情况下,-1将设置32位; -1 cast to unsigned int would be 2^31 - 1 and have only 31 bits set. -1转换为unsigned int将是2 ^ 31 - 1并且仅设置31位。

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

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