当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

查看:150
本文介绍了当int32_t是扩展整数类型并且int是32位二进制补码标准整数类型时,什么是(INT32_MIN +1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下这种情况. 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.

如果我错了,现在纠正我,但是我认为这两种类型具有相同的宽度,这意味着,根据

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):

< ...>否则,将对两个操作数执行整数提升.然后 以下规则适用于提升后的操作数: 如果两个操作数具有相同的类型,则不需要进一步的转换. 否则,如果两个操作数都具有带符号的整数类型,或者都具有无符号的 整数类型,具有较小整数转换等级的类型的操作数为 转换为排名更高的操作数的类型.

<...> 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?

在表达式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:

7.20.2指定宽度整数类型的限制

7.20.2 Limits of specified-width integer types

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 -(2N-1)

在您描述的情况下,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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆