gpt4 book ai didi

c++ - C++ 标准是否要求无符号整数的最大值为 2^N-1 形式?

转载 作者:IT老高 更新时间:2023-10-28 21:51:10 26 4
gpt4 key购买 nike

对于 T这样std::is_integral<T>::value && std::is_unsigned<T>::valuetrue ,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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com