For T
so that std::is_integral<T>::value && std::is_unsigned<T>::value
is true
, does the C++ standard guarantee that :
std::numeric_limits<T>::max() == 2^(std::numeric_limits<T>::digits)-1
in the mathematical sense? I am looking for a proof of that based on quotes from the standard.
C++ specifies the ranges of the integral types by reference to the C standard. The C standard says:
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.
Moreover, C++ requires:
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.
Putting all this together, we find that an unsigned integral type has n value bits, represents the values in the range [0, 2n) and obeys the laws of arithmetic modulo 2n.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With