Java - Bitwise signed right shift operator >>

What is Bitwise signed right shift operator?

The bitwise signed right shift operator >> shifts all the bits to the right by the number specified as its right-hand operand.

If the most significant digit of the left-hand operand is 1 (negative numbers), all higher order bits are filled with 1s after the shift operation.

If the most significant bit is 0 (positive numbers), all higher order bits are filled with 0s.

Since the sign bit after right shift operation >> remains the same, it is called a signed right shift operator.

For example, 13 >> 4 results in zero. -13 >> 4 is -1.

13 in binary form is 00000000 00000000 00000000 00001101.

00000000 00000000 00000000 00001101
shift right 4 bit
00000000 00000000 00000000 00000000 (1101 is shifted away)

public class Main {
  public static void main(String[] args) {
    int  i = 13;

    System.out.println(i >> 4);
    
  }
}

-13 in binary form is 11111111111111111111111111110011


11111111111111111111111111110011
shift right 4 bit
11111111111111111111111111111111 (0011 is shifted away)

Demo

public class Main {
  public static void main(String[] args) {
    int  i = -13;

    System.out.println(i >> 4);
    /*from  w  ww.  j  av  a2  s. c o  m*/
  }
}

Result