Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the C++ standard require the maximum of unsigned integer numbers to be of the form 2^N-1?

Tags:

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.

like image 834
Vincent Avatar asked Jan 18 '16 15:01

Vincent


1 Answers

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.

like image 121
Kerrek SB Avatar answered Oct 12 '22 23:10

Kerrek SB