I have heard that shifting into signed bit of an integer, i.e.
int test = INT_MAX;
test = (test<<1) + 1;
is undefined behaviour due to test being greater than INT_MAX. But will this behaviour be encountered in signed short variables, i.e.
short test1 = SHRT_MAX;
test1 = (test1<<1) + 1;
?
At the moment I have not come across any relevant documentation.
EDIT: I know that undefined behaviour will result in the case of integers, but not of short.
shorts will be promoted toints before shifting, which adds additional complexity here.MAX_INT >= MAX_SHORTshould always be true, should guarantee a correct representation of ashortin anint. But when it come to the shift the behavior depends on machine and specific instructions coded by the compiler. I.e. a negative number can become positive right shifted, and the opposite for left shift. But what if CPU uses SAR/SAL arithmetic shifts instead?