Shifting with signed integers is implementation defined, but if the architecture you're using has an arithmetic shift, you can pretty reliably guess it with use it.
It's because of how negative numbers are stored in the computer. It's called two's complement. To switch the sign of a two's complement, you NOT
its bits and add 1. For example, with an 8 bit integer 00011010
(26), first you NOT
to get 11100101
, then you add 1 and get 11100110
(-26). The problem comes from that most significant bit being set. If when you shifted it put 0s in at the left, the number would become positive, but if it put 1s, then the lowest possible result is 11111111
which is -1. This is how arithmetic shifts work, when you shift the computer adds bits that are the same as the left most bit.
So to be explicit, this is what is happening (using 8 bit integers because it's easier and the size is arbitrary in this case): 11111011
gets shifted 3 to the right (so 011
goes away) and since the most significant bit is set 3 1
s are inserted at the top, so you get 11111111
which is -1.