C++ 标准是否要求无符号整数的最大值为 2^N-1 的形式?
Does the C++ standard require the maximum of unsigned integer numbers to be of the form 2^N-1?
对于 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
数学意义上的?我正在寻找基于标准引用的证明。
我相信这暗示了 [basic.fundamental]/4
(N3337):
Unsigned integers, declared unsigned
, shall obey the laws of arithmetic modulo 2^n
where n
is the number
of bits in the value representation of that particular size of integer.
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, 2[=19范围内的值=]n) 并遵守算术模 2n.
的定律
对于 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
数学意义上的?我正在寻找基于标准引用的证明。
我相信这暗示了 [basic.fundamental]/4
(N3337):
Unsigned integers, declared
unsigned
, shall obey the laws of arithmetic modulo2^n
wheren
is the number of bits in the value representation of that particular size of integer.
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, 2[=19范围内的值=]n) 并遵守算术模 2n.
的定律