Why is -1 right shift 1 = -1 in Java?

bit-manipulation, java

Solution

When you do a normal right-shift (i.e. using `>>`, also known as an arithmetic right shift, as opposed to `>>>`, which is a logical right shift), the number is sign extended.

How this works is as follows:

When we right-shift we get an empty spot in front of the number, like so:

 11111111111111111111111111111111
 ?1111111111111111111111111111111(1)  (right-shift it one place)

The last `1` is shifted out, and in comes the `?`.

Now, how we fill in the `?` is dependent on how we shift.

If we do a logical shift (i.e. `>>>`), we simply fill it with `0`. If we do a arithmetic shift (i.e. `>>`), we fill it with the first bit from the original number, i.e. the sign bit (since it's `1` if the number is negative, and `0` if not). This is called sign extension.

So, in this case, `-1 >> 1` sign-extends the `1` into the `?`, leaving the original `-1`.

Further reading:

- Arithmetic shift on Wikipedia

- Logical shift on Wikipedia

Problem

I came across the question "Why is -1 zero fill right shift 1=2147483647 for integers in Java?" I understood the concept of zero fill right shift perfectly well from the above question's answer. But when I tried to find -1>>1, I am getting a totally complex answer which I felt difficult to understand. ``` -1 in binary form is as follows: 11111111111111111111111111111111 After flipping the bits, I got: 00000000000000000000000000000000 Upon adding 1 to it, I got: 00000000000000000000000000000001 Now shifting one position right: 00000000000000000000000000000000 After flipping the bits, I got: 11111111111111111111111111111111 Now adding 1 to it: 00000000000000000000000000000000 ``` I don't understand how -1>>1 is -1 itself, then?

Original source

Related problems