Q:

Unexpected behavior while dealing with shift operators

My program is written below:
void main() {
   int n =0;
   printf("%x", (~0 << (32+ (~n +1) )));
}

As n = 0, ~n = 0xffffffff == -1, so ~n + 1 is equal to 0. 
When I execute this program, I get 0xffffffff, which is incorrect as (~0 << 32 ) outputs 0. 
When I replace (~n +1) with 0, it outputs 0.   
Any help is very much appreciated.

A:

You're shifting a 32-bit wide value by 32 bits.
The result is undefined and could equal mushroom lasagna for all you know.

[C99: 6.5.7/3]: The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.

Any further analysis, then, is folly.

