对于 T
这样std::is_integral<T>::value && std::is_unsigned<T>::value
是 true
,C++ 标准是否保证:
std::numeric_limits<T>::max() == 2^(std::numeric_limits<T>::digits)-1
在数学意义上?我正在寻找基于标准引用的证明。
最佳答案
C++ 通过引用 C 标准来指定整数类型的范围。 C 标准说:
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.
此外,C++ 要求:
Unsigned integers shall obey the laws of arithmetic modulo 2n where n is the number of bits in the value representation of that particular size of integer.
综上所述,我们发现无符号整数类型有 n 个值位,表示范围为 [0, 2n) 并遵守算术模 2n 的定律。
关于c++ - C++ 标准是否要求无符号整数的最大值为 2^N-1 形式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34858829/