Search code examples
c++language-lawyerbit-shiftinteger-promotionunsigned-long-long-int

Implicit conversion: is the following warning valid?


This question Implicit type conversion rules in C++ operators (and several others) state

If either is long long unsigned int the other is promoted to long long unsigned int

However if I do the following under MSVC:

unsigned int a = <some expression>;
unsigned long long b = a << 32ULL;

The second line generates the following warning:

warning C4293: '<<': shift count negative or too big, undefined behavior

32ULL is a 64 bit unsigned value, therefore according to the implicit conversion rules this should mean that a is converted to unsigned long long as well. Hence I'm shifting a 64 bit value by 32 bits, clearly a well defined operation.

Is MSVC bugged or is there a flaw in my logic?


Solution

  • Shifts don't do the so-called "usual arithmetic conversions", which is the rules you cited. They only perform integral promotions. The result of a shift is of the same type as the promoted left operand.