10

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?

curiousguy
  • 7,344
  • 2
  • 37
  • 52
dgnuff
  • 2,669
  • 1
  • 15
  • 26
  • 1
    Shifts are special. – T.C. Aug 15 '17 at 18:00
  • Visual Studio 2015. That said, I'm 99.9% certain that we got the same warning from VS 2010 before upgrading, and a quick test with VS 2017 also gives the same warning. – dgnuff Aug 15 '17 at 18:02
  • This needs the language lawyer tag. This is the subtle difference between 'usual arithmetic conversions' and 'integral promotions'. They're different things but both happen and both may convert values. – Persixty Aug 15 '17 at 18:14

1 Answers1

15

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.

T.C.
  • 123,516
  • 14
  • 264
  • 384