Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C bitwise-shift: right operand considered for implicit type-conversion?

gcc 4.8.4 warns about 1u << 63ul​ (assuming 64 bit long and 32 bit int) and computes 0. Rightfully so (no promotion from 1u​ to 1ul before shifting)?

ISO/IEC 9899:201x, 6.3.1.8 (Usual arithmetic conversions): "Many operators that expect operands of arithmetic type cause conversions"; 6.5.7 (Bitwise shift operators): "The integer promotions are performed on each of the operands...".

But I am not unable to conclude. Which are those "many operators"? As I understand, "integer promotion" does not pertain to types wider than int (am I correct?), but the standard does not explicitly state that the right operand of a bitwise-shift is not taken into account for the implicit type conversion.

like image 630
Loic Avatar asked Nov 29 '25 14:11

Loic


1 Answers

Each operation documents this separately. For example, n1548 §6.5.5 "Multiplicative operators" ¶3

The usual arithmetic conversions are performed on the operands.

This phrase is omitted from §6.5.7 "Bitwise shift operators". Instead it says:

The integer promotions are performed on each of the operands. The type of the result is the type of the promoted left operand. …

Since the section on bitwise shift operators says nothing about "usual arithmetic conversions", that conversion does not happen.

like image 155
Dietrich Epp Avatar answered Dec 02 '25 04:12

Dietrich Epp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!