I have the following c code:
int foo = 0;
printf("output1: %08x\n", (~0x0) << (~0));
printf("output2: %08x", (~0x0) << (~foo));
which prints out:
output1: ffffffff
output2: 80000000
Why does shifting by the same number produce a different result?
Basically your snippet is an orgy in undefined behavior.
0x0
literals as well as variables of type int
are signed, meaning they can get negative values. ~0
is always a negative value.Therefore anything can happen in this program, including the whole thing crashing and burning. As a rule of thumb, never use signed variables together with bitwise operators.