I am trying to understand arithmetic overflow. Suppose I have the following,
unsigned long long x;
unsigned int y, z;
x = y*z;
y*z can lea
If one operand is wider than the other, the compiler should be (or behave as if it is) converting both operands to the same size, so casting one to a larger size will produce the correct behaviour.
This is specified in the C and C++ standards. The C++11 standard (n3337 draft) has this to say, in chapter five, statement 9:
... if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank shall be converted to the type of the operand with greater rank.
There is a couple of pages describing all the conversions and stuff that goes on, but this is what defines this particular expression's behaviour.