When the negative numbers are stored, they use2's complement form.Signed right shifting >>
Here the sign of the number will not be changed due to shifting. The sign bit will make copies and propagate to the right side when the bits are shifted. An example below.
var binary= (7>>1).toString();
How we got 3 is clear as its simple right shift. The -4 came instead of -3 because it uses 2's complement. That details are there in one of myprevious post.Unsigned right shifting >>>
Lets see what will happen in unsigned shifting. The main difference is that the sign bit just won't propagate to the right side without making copy. Hence the sign will change. Since the bits added to the left are 0s the sign of -ve number will change to +ve.
var binary= (7>>>1).toString();
The negative number became positive. Lets see how that happened.
11111111 11111111 11111111 11111001 - 2's complement of -7
01111111 11111111 11111111 11111100 - After shifting bits one time to right
Look at the left most sign bit it changed to 0 which means its a +ve number now. What is that number? Its 2147483644.Left shifting <<
The point to remember here is that the unsigned shifting is not there for left shifting operation. The sign bit can be overwritten by the bit right to that and that becomes the new sign of the number.
var binary= (1<<31).toString
How this -ve number came?
00000000 00000000 00000000 00000001 - 1 in 32 bit rep
10000000 00000000 00000000 00000000 - shifted left 31 times
The sign bit is negative hence its in 2's complement. Get the number from this 2's complement format. Its -2147483648References