Imagine this situation. int32_t
is an extended integer type and it's represented in two's complement (as the standard required int32_t
to be represented). This means that INT32_MIN
is -2147483648
(0x80000000
).
Meanwhile int
is a standard integer type and it's represented in one's complement (as the standard allows). This means that INT_MIN
is -2147483647
.
Now correct me if I'm wrong, but I think both types have the same width, which means, according to 6.3.1.1.1 (emphasis mine):
The rank of any standard integer type shall be greater than the rank of any extended integer type with the same width.
So the rank of int32_t
is lower than that of int
.
Now 6.3.1.8 (usual arithmetic conversions) says (emphasis mine):
<...> Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands: If both operands have the same type, then no further conversion is needed. Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
So if understand it correctly, in this code block:
int32_t x = INT32_MIN;
int y = 1;
x + y; // What happens here?
In the expression x + y
, x
has to be promoted to int
, and INT32_MIN
is outside of the range of int
.
Is this a bug in the standard or am I missing something?
In other words, what does the expression x + y
in this context evaluate to, as defined by the standard?
INT32_MIN
should be equal to -(2^31). It's rather bizarre for it to be odd. – cHaoMIN
values are one lower (further from zero) than they should be. One's complement would have odd minimums and maximums, the min would be-(2^31) + 1
. – ShadowRangerint32_t
to be different complement thantint
". Please provide a standard quote that explicitly (or implicitly) prohibits it. – MarkWestonint
(other than requiring it at least be able to represent integers from -32767 to 32767). It could be 16 bits, or 32, or 64, or even something like 18 or 36. There's nothing requiring thatint32_t
andint
be the same underlying type or have the same size. That's one of the biggest reasons the exact-width integer types even exist. (The other reason is to be able to specify two's complement.) – cHaoINT_MIN
can be -(2^31) if the implementation chooses to extend the range. Only the exact-width types have their minimums and maximums defined, mostly because those are the only ones where the size and representation are fully specified (and thus the only ones where the limits are etched in stone). – cHao