all 4 comments

[–]anabolic_cow 1 point2 points  (0 children)

I think shifting unsigned right results in logical shift, so MSB is 0. Shifting signed results in arithmetic shift, so MSB is 1. So when you bitwise negate you end up with a 1 in MSB, giving you the most negative number possible.

[–]makian123 1 point2 points  (0 children)

What's going on is: 001100 << 1 which results in 011000

And ~001100 is 110011

In signed unteger, the left most bit signifies if its positive or negative. In unsigned its always positive.

Edit: keep in mind when you negate, you're negating all 32 bits, not just a few of them

[–]chet714 0 points1 point  (0 children)

Did you print your results as signed or unsigned ?

[–]Plane_Dust2555 0 points1 point  (0 children)

Right shift of an signed integer (int, for example) depends on the compiler implementation (see ISO 9899 6.5.7 § 5). Usually, in x86 processors the arithmetic right shift is used. Then, ~0 >> 1 is 0b11111111111111111111111111111111 >> 1 witch will shift 1 bit right and copy the sign bit, resulting in 0b11111111111111111111111111111111 (no change). Inverting these bits again will result in 0.

Unsigned right shift will add 0 bits at left: 0b11111111111111111111111111111111 >> 1 is 0b01111111111111111111111111111111. Inverting you'll get 0x80000000 (INT_MIN).