符号和无符号整型整数转换等级 [英] Integer conversion rank of signed and unsigned int

查看:176
本文介绍了符号和无符号整型整数转换等级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我有,

 int a = 42;
 unsigned b = 10;
 int c = a + b;

对于这一说法, INT C = A + B; 应该 A 先转换为 unsigned int类型或会是 b 将被转换为符号int ?无论 unsigned int类型签署已经在同级别的转换让我们怎么知道哪一个会被转换?有没有一个标准规则?

For this statement, int c = a + b; Would a be first converted to an unsigned int or would it be b that will be converted to a signed int? Both unsigned int and signed have the same conversion rank so how do we know which one will be converted? Is there a Standard rule?

推荐答案

简短的回答:每C99 6.3.1.8-P1, A 的价值将被转换为一个无符号通过诠释每C99 6.3.1.3-P2,其UINT_MAX + 1添加到它,直到它落在由 unsigned int类型允许的范围。因为它已经在该范围内,没有另外将被执行。通过C99 6.3.1.3-P3,结果赋值给诠释三是,如果(P1)和(P2)没有申请实现定义。但是,在这种情况下,注意6.3.1.3-P1的价值的条款。在这种情况下值(52)的可以的是由 INT psented重新$ P $,所以也没有改变,而的定义。

Short answer: Per C99 6.3.1.8-p1, as value will be converted to an unsigned int by, per C99 6.3.1.3-p2, having UINT_MAX+1 added to it until it falls in the range allowed by unsigned int. Since it is already in that range, no addition will be performed. By C99 6.3.1.3-p3, the results assigned back to int c would be implementation defined if (p1) and (p2) didn't apply. But in this case note the "value" clause of 6.3.1.3-p1. The value (52) in this case can be represented by int, so it is not changed, and is defined.

C99 6.3.1.3符号和无符号整数


      
  1. 当用整数类型的值被转换为比_Bool其他另一个整数类型,如果该值可再被新类型psented $ P $,这是不变的。

  1. When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.

否则,如果新类型是无符号的值被重复地增加或减1比可以重新$ P $在新型psented直到值在所述范围内的最大值更转换新type.60)

Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.60)

否则,新的类型签名和值不能再次在它psented $ P $;无论结果是实现定义或实现定义的信号被提升。

Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.

C99 6.3.1.8通常的算术转换

这是期望算术类型转换的原因和产量结果类型的操作数以类似的方式许多运营商。目的是要确定用于操作数和结果的共同实际类型。对于指定的操作,每个操作数转换,没有类型域的变化,一个类型,其对应的实际类型是常见的真正类型。除非明确说明,否则,常见的真实类型也相应真实类型的结果,其类型域是操作数的类型域,如果它们是相同的,而复杂的,否则的。这种模式被称为的通常的算术转换的:

Many operators that expect operands of arithmetic type cause conversions and yield result types in a similar way. The purpose is to determine a common real type for the operands and result. For the specified operands, each operand is converted, without change of type domain, to a type whose corresponding real type is the common real type. Unless explicitly stated otherwise, the common real type is also the corresponding real type of the result, whose type domain is the type domain of the operands if they are the same, and complex otherwise. This pattern is called the usual arithmetic conversions:

首先,如果相应的实型操作数是long double,则另一个操作数转换,没有类型域的变化,一个类型,其对应的实际类型为长双。

First, if the corresponding real type of either operand is long double, the other operand is converted, without change of type domain, to a type whose corresponding real type is long double.

否则,如果相应的实型操作数的两倍,其他的将被转换,没有类型域的变化,一个类型,其对应的实际类型是双。

Otherwise, if the corresponding real type of either operand is double, the other operand is converted, without change of type domain, to a type whose corresponding real type is double.

否则,如果相应的实型操作数是浮点数,另一个将被转换,没有类型域的变化,一个类型,其对应的实际类型是float.62)

Otherwise, if the corresponding real type of either operand is float, the other operand is converted, without change of type domain, to a type whose corresponding real type is float.62)

另外,整数促销活动是在两个操作数执行。然后下面的规则被施加到推动操作数:

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.

否则,如果具有无符号整型操作数的秩大于或等于另一个操作数的类型的秩,然后用符号整型操作数被转换为操作数的无符号整数的类型类型。

Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.

否则,如果操作数的有符号整数类型的类型可以重新present所有无符号整型操作数的类型的值,然后用无符号整型操作数转换为同类型符号整型的操作数。

Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.

否则,两个操作数都转换为对应于与符号整型的操作数的类型的无符号整型。

Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

这篇关于符号和无符号整型整数转换等级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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