与运算符的整数促销<< [英] Integer promotion with the operator <<

查看:105
本文介绍了与运算符的整数促销<<的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于问题 Bitshift和整数提升?,我对整数提升有疑问当使用左移位时.

Similar to the question Bitshift and integer promotion?, I have a question about integer promotion when using left bitshifts.

unsigned int test(void)
{
  unsigned char value8;
  unsigned int result;

  value8 = 0x12;
  result = value8 << 8;
  return result;
}

在这种情况下,value8将首先提升为unsiged int还是特定于实现?

6.5.7按位移位运算符 ... 3 Sematics ...
对每个操作数执行整数提升.结果的类型是 提升后的左操作数的值.如果右操作数的值为负或为 大于或等于提升的左操作数的宽度,则行为是不确定的.

6.5.7 Bitwise shift operators ... 3 Sematics ...
The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.

它表示对每个操作数执行整数提升." ,但是提升规则在这里是什么?

It says that the "The integer promotions are performed on each of the operands.", but what is here the promotion rule?

我认为它应该是convert to int if lesser rank than int,但找不到.

I assume that it should be convert to int if lesser rank than int, but I can't find it.

我问这个问题,因为一个编译器(Renesas nc30wa)不会提升为int,所以样本的结果始终为0.

I ask this, as one compiler (Renesas nc30wa) doesn't promote to int, so the result is always 0 for my sample.

在此平台上,char是8位宽,而int是16位.

On this platform, a char is 8 bit wide and int 16 bits.

推荐答案

短语整数促销"是非常的特定内容,可在(对于C99)第6.3.1.1 Booleans, characters, and integers部分中找到:

The phrase "the integer promotions" is a very specific thing, found in (for C99) section 6.3.1.1 Booleans, characters, and integers:

如果int可以表示原始类型的所有值,则该值将转换为int;否则,该值将转换为int.否则,它将转换为unsigned int.这些称为整数促销.整数促销未更改所有其他类型.

If an int can represent all values of the original type, 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.

因此,假设您的unsigned char可以保存在int中,它将被提升为int.在那些unsigned charint一样宽的稀有平台上,它将提升为unsigned int.

So assuming your unsigned char can be held in an int, it will be promoted to an int. On those rare platforms where unsigned char is as wide as an int, it will promote to an unsigned int.

这仅在C11中稍有改变:

This is only changed slightly in C11:

如果int可以表示原始类型的所有值(受位字段的宽度限制),则该值将转换为int;否则,该值将转换为int.否则,它将转换为unsigned int.这些称为整数促销.整数促销未更改所有其他类型.

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.

如果特定的编译器没有遵循此行为,则说明它实际上不是合规的.但是,鉴于您列出的编译器是针对嵌入式系统的,所以这并不奇怪.

If a specific compiler doesn't follow this behaviour, then it's not really conforming. However, given that the compiler you listed is for embedded systems, it's not really surprising.

许多产品是为特定目的而制造的,一致性在要求列表中并不总是很高.可能会有一些编译器标志,这些标志将使其更紧密地符合标准.

Many are built for specific purposes and conformance is not always high on the list of requirements. There may be compiler flags that will allow it to more closely conform to the standard.

根据您的特定环境M16C Series,R8C Family C Compiler Package V.5.45 C Compiler2.1.4 nc30 Command Line Options部分中有f. Generated code modification options小节:

Looking at your particular environment, the M16C Series,R8C Family C Compiler Package V.5.45 C Compiler has, in section 2.1.4 nc30 Command Line Options, subsection f. Generated code modification options:

-fextend_to_int-fETI::将char类型的数据扩展到int类型之后执行操作.根据ANSI标准进行了扩展.

-fextend_to_int, -fETI: Performs operation after extending char-type data to the int type. Extended according to ANSI standards.

尽管我怀疑-fansi可能是一个更好的选择,因为它还涵盖了其他一些内容.

although I suspect -fansi is probably a better choice since it covers a few other things as well.

这篇关于与运算符的整数促销&lt;&lt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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