这个问题以前被问过,但我仍然很困惑。
我知道
unsigned int a = -1;
将会UINT_MAX
。但这并不是因为所有 -1 位都已设置。 C11 说
如果新类型是无符号的,则通过重复添加或来转换该值
比新类型可以表示的最大值减一
直到该值在新类型的范围内
所以可以说UINT_MAX
是 100 (我知道它应该大于 2^16-1 但现在让我们忽略它)
unsigned int a = -1; // will be
unsigned int a = -1 + UINT_MAX + 1; // 100 = UINT_MAX
标准只说UINT_MAX >= 2^16-1
。但它有说任何地方都应该是 2^n-1 吗?
C++ 中的答案也不同吗?
In C, the maximum value for an unsigned integer must be in the form1: 2N - 1.
因此所有value值 UINT_MAX 的位将被设置为 1。可能有padding位,其值未指定。
1 (Quoted from: ISO/IEC 9899:201x 6.2.6.2 Integer types 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). If there are N value bits, each bit shall represent a different power of 2 between 1 and 2N−1 , so that objects of that type shall be capable of representing values from 0 to 2N−1 using a pure binary representation; this shall be known as the value representation. The values of any padding bits are unspecified.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)