当 int32_t 是扩展整数类型且 int 是 32 位补码标准整数类型时, (INT32_MIN + 1) 是什么
What is (INT32_MIN + 1) when int32_t is an extended integer type and int is 32-bit one's complement standard integer type
想象一下这种情况。 int32_t
是一个 扩展整数类型 ,它以二进制补码表示(作为标准要求 int32_t
表示)。这意味着 INT32_MIN
是 -2147483648
(0x80000000
).
同时 int
是一个 标准整数类型 并且它用补码表示(标准允许)。这意味着 INT_MIN
是 -2147483647
.
如果我错了,请纠正我,但我认为这两种类型 具有相同的宽度 ,这意味着,根据 6.3.1.1.1(强调我的):
The rank of any standard integer type shall be greater than the rank of any extended integer type with the same width.
所以int32_t
的排名低于int
。
现在 6.3.1.8(通常的算术转换)说(强调我的):
<...> 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.
所以如果理解正确的话,在这个代码块中:
int32_t x = INT32_MIN;
int y = 1;
x + y; // What happens here?
在表达式x + y
中,x
必须提升为int
,而INT32_MIN
超出了int
的范围。
这是标准中的错误还是我遗漏了什么?
换句话说,根据标准的定义,表达式 x + y
在这种情况下的计算结果是什么?
Meanwhile int is a standard integer type and it's represented in one's
complement
扩展整数是实现定义的,只能是二进制数,有符号整数需要用一个补码、二进制补码或符号和大小表示法表示
intxx_t 是固定大小的 int 类型,它必须具有与 int 相同的表示形式。因为 intxx_t 是二进制补码,所以 int 必须相同。
int32_t
是可选的。符合规范的实现不能有 32 位二进制补码 int
和 32 位二进制补码扩展整数类型 int32_t
;如果 int
是补数,则很可能不会提供 int32_t
。
这是 32 位二进制补码 int
和 32 位二进制补码扩展整数类型 int32_t
不能共存的原因之一。引用 N1570 draft:
7.20.2 Limits of specified-width integer types
1 The following object-like macros specify the minimum and maximum limits of the types
declared in <stdint.h>
. Each macro name corresponds to a similar type
name in 7.20.1.
2 Each instance of any defined macro shall be replaced by a constant
expression suitable for use in #if preprocessing directives, and this
expression shall have the same type as would an expression that is an
object of the corresponding type converted according to the integer
promotions. Its implementation-defined value shall be equal to or
greater in magnitude (absolute value) than the corresponding value
given below, with the same sign, except where stated to be exactly the
given value.
...
INTN_MIN exactly -(2N-1)
在您描述的情况下,INT32_MIN
的值必须恰好为 -2^31,但由于整数提升,它必须具有无法容纳该值的类型。这种矛盾根本无法提供 int32_t
。
想象一下这种情况。 int32_t
是一个 扩展整数类型 ,它以二进制补码表示(作为标准要求 int32_t
表示)。这意味着 INT32_MIN
是 -2147483648
(0x80000000
).
同时 int
是一个 标准整数类型 并且它用补码表示(标准允许)。这意味着 INT_MIN
是 -2147483647
.
如果我错了,请纠正我,但我认为这两种类型 具有相同的宽度 ,这意味着,根据 6.3.1.1.1(强调我的):
The rank of any standard integer type shall be greater than the rank of any extended integer type with the same width.
所以int32_t
的排名低于int
。
现在 6.3.1.8(通常的算术转换)说(强调我的):
<...> 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.
所以如果理解正确的话,在这个代码块中:
int32_t x = INT32_MIN;
int y = 1;
x + y; // What happens here?
在表达式x + y
中,x
必须提升为int
,而INT32_MIN
超出了int
的范围。
这是标准中的错误还是我遗漏了什么?
换句话说,根据标准的定义,表达式 x + y
在这种情况下的计算结果是什么?
Meanwhile int is a standard integer type and it's represented in one's complement
扩展整数是实现定义的,只能是二进制数,有符号整数需要用一个补码、二进制补码或符号和大小表示法表示
intxx_t 是固定大小的 int 类型,它必须具有与 int 相同的表示形式。因为 intxx_t 是二进制补码,所以 int 必须相同。
int32_t
是可选的。符合规范的实现不能有 32 位二进制补码 int
和 32 位二进制补码扩展整数类型 int32_t
;如果 int
是补数,则很可能不会提供 int32_t
。
这是 32 位二进制补码 int
和 32 位二进制补码扩展整数类型 int32_t
不能共存的原因之一。引用 N1570 draft:
7.20.2 Limits of specified-width integer types
1 The following object-like macros specify the minimum and maximum limits of the types declared in
<stdint.h>
. Each macro name corresponds to a similar type name in 7.20.1.2 Each instance of any defined macro shall be replaced by a constant expression suitable for use in #if preprocessing directives, and this expression shall have the same type as would an expression that is an object of the corresponding type converted according to the integer promotions. Its implementation-defined value shall be equal to or greater in magnitude (absolute value) than the corresponding value given below, with the same sign, except where stated to be exactly the given value.
...
INTN_MIN exactly -(2N-1)
在您描述的情况下,INT32_MIN
的值必须恰好为 -2^31,但由于整数提升,它必须具有无法容纳该值的类型。这种矛盾根本无法提供 int32_t
。