What difference between signed left and unsigned right shift operators

What difference between signed left and unsigned right shift operators ?

or
What is difference between the >> and >>> operators ?

Answer :
Signed left shift operator(<<) : It shifts a bit pattern to the left
Signed right shift operator (>>) : It shifts a bit pattern to the right
Unsigned right shift operator (>>>) : It shifts a zero into the leftmost position and while leftmost position after “>>” depends on sign extension

Please see example below:

package com.javahonk.shiftoperator;

public class ShiftOperatorTest {

	public static void main(String[] args) {

	System.out.println("Right shift opeartor(>>)");
	System.out.println("  46 >> 3 = " + (46 >> 3));
	System.out.println(" -46 >> 3 = " + (-46 >> 3));

	System.out.println("Left shift opeartor (<<)");
	System.out.println(" -46 >> 3 = " + (-46 << 3));
	System.out.println("  46 >> 3 = " + (46 << 3));

	System.out.println("Unsigned right shift opeartor (>>>)");
	System.out.println(" -46 >>> 3 = " + (-46 >>> 3));
	System.out.println("  46 >>> 3 = " + (46 >>> 3));

  }

}

 

Output:

What difference between signed left and unsigned right shift operators

 

Leave a Reply

Your email address will not be published. Required fields are marked *