If got the following code:
uint8_t value = 0xF0;
uint16_t shift = 0x10;
uint32_t result = value << shift;
cout << "The Result is: " << result << endl;
I expected that the the output to be 0 but instead it was 15728640.
My Questions
- Isn’t the expresion evaluated => 0 and after that implicitly converted
to the type of result? - Is this behaviour standard?
- Where can I get further information about this kind of behaviour?
When you write
value << shift,valueis implicitly converted tointbefore the shift. This is called “integer promotion”. No other rules apply to this particular case.Integer promotion applies to any type whose range fits within an
int. If it can’t fit in anint, thenunsigned intis tried. If it can’t fit in either, then integer promotion does nothing.For more information… well, I just read the spec.