I have spent some time trawling this site; in particular this question: Is ((a + (b & 255)) & 255) the same as ((a + b) & 255)?
In doing so, I've been led to the conclusion that
int main()
{
unsigned short i = std::numeric_limits<unsigned short>::max();
unsigned short j = i;
auto y = i * j;
}
can lead to undefined behaviour due to a type promotion of i
and j
to int
which subsequently overflows upon the multiplication! Perhaps i
and j
need not to even be this large.
My conclusion is that, for example, on a system where unsigned short
is 16 bits and int
is 32 bits, the behaviour can be undefined.
Am I correct here?