问题描述
想象一下这种情况. int32_t
是扩展的整数类型,它用二进制补码表示(作为表示需要的int32_t
的标准).这意味着INT32_MIN
是-2147483648
(0x80000000
).
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
).
同时int
是标准整数类型,它用一个补码表示(在标准允许的范围内).这意味着INT_MIN
是-2147483647
.
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
.
如果我错了,现在纠正我,但是我认为这两种类型具有相同的宽度,这意味着,根据 6.3.1.1.1 (强调我的意思):
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):
所以int32_t
的等级低于int
的等级.
So the rank of int32_t
is lower than that of int
.
现在6.3.1.8(通常是算术转换)说(强调我的意思):
Now 6.3.1.8 (usual arithmetic conversions) says (emphasis mine):
因此,如果正确理解,请在此代码块中:
So if understand it correctly, in this code block:
int32_t x = INT32_MIN;
int y = 1;
x + y; // What happens here?
在表达式x + y
中,必须将x
提升为int
,并且INT32_MIN
不在int
的范围内.
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?
换句话说,按照标准定义,在这种情况下,表达式x + y
的计算结果是什么?
In other words, what does the expression x + y
in this context evaluate to, as defined by the standard?
推荐答案
int32_t
是可选的.符合的实现不能具有32位二进制补码int
和32位二进制补码扩展的整数类型int32_t
;如果int
是某人的补语,则很可能不会提供int32_t
.
int32_t
is optional. A conforming implementation cannot have 32-bit one's complement int
and 32-bit two's complement extended integer type int32_t
; if int
is one's complement, int32_t
would most likely not be provided.
这是32位二进制补码int
和32位二进制补码扩展整数类型int32_t
无法共存的原因之一.引用 N1570草案:
Here's one reason 32-bit one's complement int
and 32-bit two's complement extended integer type int32_t
can't coexist. Quoting the N1570 draft:
1以下类似对象的宏指定了类型的最小和最大限制 在<stdint.h>
中声明.每个宏名称对应一个相似的类型 在7.20.1中命名.
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任何已定义宏的每个实例都应用一个常量替换 适用于#if预处理指令和 this的表达式 表达式应与作为表达式的表达式具有相同的类型 根据整数转换的相应类型的对象 促销.其实现定义的值应等于或 大小(绝对值)大于相应的值 除非另有说明,否则以下给出的符号相同 给定值.
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 -(2)
在您描述的情况下,INT32_MIN
必须具有正好为-2 ^ 31的值,但是由于整数促销,它必须具有无法容纳该值的类型.这种矛盾根本无法提供int32_t
.
In the situation you describe, INT32_MIN
must have value exactly -2^31, but due to the integer promotions, it must have a type that cannot hold that value. This contradiction prevents providing int32_t
at all.
这篇关于当int32_t是扩展整数类型并且int是32位二进制补码标准整数类型时,什么是(INT32_MIN +1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!