Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are C++ Standard guarantees on relation between min and max values of signed integer types?

Is it safe to assume that -LLONG_MAX (negated LLONG_MAX) belongs to long long range?

Is it safe to assume that if LLONG_MIN < -LLONG_MAX then LLONG_MIN == -LLONG_MAX - 1?

Is it guaranteed by the Standard or just all actual devices provide either LLONG_MIN == -LLONG_MAX - 1 or LLONG_MIN == -LLONG_MAX?

like image 920
Fyodor Menshikov Avatar asked Jul 19 '16 18:07

Fyodor Menshikov


1 Answers

Is it safe to assume that -LLONG_MAX (negated LLONG_MAX) belongs to long long range?

Is it safe to assume that if LLONG_MIN < -LLONG_MAX then LLONG_MIN == -LLONG_MAX - 1?

Is it guaranteed by the Standard or just all actual devices provide either LLONG_MIN == -LLONG_MAX - 1 or LLONG_MIN == -LLONG_MAX?


Those three statements are true in the case the implementation uses one of 2's complement, 1's complement, or sign and magnitude to represent signed integer types. -LLONG_MAX is in the range of long long in all three schemes, and LLONG_MIN is -LLONG_MAX (1's complement, sign and magnitude, and possibly 2's complement) or is -LLONG_MAX-1 (possibly 2's complement). 2's complement machines might use that extra value as a trap representation, just as 1's complement and sign and magnitude machines might use negative zero as a trap representation. So the answer to your questions is "yes" if the standard mandates that an implementation use one of those schemes.

The C standard (to which the C++ standard defers in many places) mandates either 2's complement, 1's complement, or sign and magnitude:

C11 6.2.6.2 Integer types:

If the sign bit is one, the value shall be modified in one of the following ways:

— the corresponding value with sign bit 0 is negated (sign and magnitude);
— the sign bit has the value −(2M ) (two’s complement);
— the sign bit has the value −(2M − 1) (ones’ complement).

The C++ standard appears to be a bit more open:

C++14 3.9.1 Fundamental types:

The representations of integral types shall define values by use of a pure binary numeration system51. [Example: this International Standard permits 2’s complement, 1’s complement and signed magnitude representations for integral types. —end example]

Footnote 51, which defines what "a pure binary numeration system" means, appears to rule out decimal systems, and also offset systems (where 0 isn't all bits zero):

51) A positional representation for integers that uses the binary digits 0 and 1, in which the values represented by successive bits are additive, begin with 1, and are multiplied by successive integral power of 2, except perhaps for the bit with the highest position.

Practically speaking, all that's left are 2's complement, 1's complement, and sign and magnitude: The same schemes mandated by the C standard. I doubt a vendor would touch a machine that uses a new 21st century scheme to represent the integers and somehow meets the letter of the law of the C++ standard (but hence is noncompliant with C).

like image 158
David Hammen Avatar answered Nov 07 '22 15:11

David Hammen