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

标签 c++ c++11 standards language-lawyer unsigned

对于 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/

相关文章:

c++ - RabbitMQ C 库如何在 amqp_simple_wait_frame 上进行定时等待?

Boost中的c++ Hermite插值算法

c++ - 从 C++98 过渡到 C++0x

c++ - 是否有粘性操纵器的官方资源

Java XML 实践

c++ - 通过重新解释转换创建无效引用

带有函数指针的 C++ vector push_back

c++ - 使用 OpenSSL 和 C++ 生成 sha256

c++ - 模板元编程 : Sum of 1 to n

c++ - 如何解决访问不同函数的 "nonstatic member reference must be relative to a specific object"