In both of your examples, assuming 32bit int s, you invoke undefined behavior, as indicated in Why does a left shift operation cause undefined behavior when the left side operand has a negative value?
Why? Because number is a signed int , with 32 bits of memory. (3<<31) does not appear in this type.
Once you are in the area of ββundefined behavior, the compiler can do what it likes.
(You cannot rely on any of the following because this is UB - it's just watching what your compiler does).
In this case, it looks like the compiler is doing a right shift, resulting in 0x80000000 representing a binary representation. This turns out to be a two-component representation of INT_MIN . Thus, the second fragment is not surprising.
Why does the first output the same thing? In two additions MIN_INT will be -2^31 . But the maximum value is 2^31-1 . MIN_INT * -1 will be 2^31 (if it were representable). And guess what idea would be? 0x80000000 . Get back to where you started!
source share