Bitshift and integer promotion?

后端 未结 2 1424
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 11:18

Normally, C requires that a binary operator\'s operands are promoted to the type of the higher-ranking operand. This can be exploited to avoid filling code with verbose cast

相关标签:
2条回答
  • 2020-11-28 11:39

    The trouble really is that promotion only works up to whatever your platform defines as an int. As some other answers have stated, the bit-shift operator will promote the left operand to an int. However, here an int is defined as a 32-bit value. The integer conversion will not promote to a long long (64-bit).

    0 讨论(0)
  • 2020-11-28 11:54

    The so-called usual arithmetic conversions apply to many binary operators, but not all of them. For example they do not apply to the bit shift operators, &&, ||, comma operator, and assignment operators. This is the rule for the bit shift operators:

    6.5.7 ... 3 Semantics ...
    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.

    0 讨论(0)
提交回复
热议问题