integer promotion in c

时间秒杀一切 提交于 2019-12-01 10:08:01

问题


Let say I have a 32-bit machine.

I know during integer promotion the expressions are converted to:\

  • int if all values of the original type can be represented in int
  • unsigned otherwise

Could you please explain what will happen for the following expression? and In general, how ranking works here?

First snippet:

si16  x, pt;
si32  speed;
u16 length;
x = (speed*pt)/length;

Second one:

x = pt + length;

EDIT:

si16 means signed short (size 16 bit), si32 bit means signed int (size 32 bit) and u16 means unsigned short (size 16)

I found the following link that has described the issue very clearly: Implicit type conversion.

Concretely, read the answer of Lundin, very helpful!


回答1:


The integer promotion rule, correctly cited C11 6.3.1.1:

If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions. All other types are unchanged by the integer promotions.

Where "otherwise, it is converted to an unsigned int" is in practice only used in one particular special case, namely where the smaller integer type unsigned short has the same size as unsigned int. In that case it will remain unsigned.

Apart from that special case, all small integer types will always get promoted to (signed) int regardless of their signedness.


Assuming 32 bit int, then:

 x = (speed*pt)/length;

speed is signed 32, it will not get promoted. ptr will get integer promoted to int (signed 32). The result of speed*ptr will have type int.

length will get integer promoted to int. The division will get carried out with operands of type int and the resulting type will be int.

The result will get converted to signed 16 as it is assigned to x (lvalue conversion during assignment).

x = pt + length; is similar, here both operands of + will get promoted to int before addition and the result will afterwards get converted to signed 16.




回答2:


The integer promotion rules are defined in 6.3.1.8 Usual arithmetic conversions.

1.  si16  x, pt;
    si32  speed;
    u16 length;
    x = (speed*pt)/length;

2. x =  pt + length;

Ranking means effectively the number of bits from the type as defined by CAM in limits.h. The standards imposes for the types of lower rank in CAM to correspond types of lower rank in implementation.

For your code,

speed*pt

is multiplication between int32 and int 16, which means, it is transformed in

speed*(int16=>int32)pt

and the result tmp1 will be int32.

Next, it will continue

tmp1_int32 / length

Length will be converted from uint16 to int32, so it will compute tmp2 so:

tmp1_int32 / (uint16=>int32) length

and the result tmp2 will be of type int32.

Next it will evaluate an assignment expression, left side of 16 bits and the right side of 32, so it will cut the result so:

x = (int32=>int16) tmp2_int32

Your second case will be evaluated as

x = (int32=>int16) ( (int16=>int32) pt + (uint16=>int32) length )

In case an operator has both operands with rank smaller than the rank of int, the CAM allows to add both types if the operation does not overflow and then to convert the result to integer.

In other words, it is possible to covert INT16+INT16 either in

 INT16+INT16

or in

 (int32=>int16) ((int16=>int32) INT16 + (int16=>int32)INT16)

provided the addition can be done without overflow.



来源:https://stackoverflow.com/questions/44455248/integer-promotion-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!