Java Right Shift Operator
Chapter:
Operators
Last Updated:
13-12-2016 15:46:37 UTC
Program:
/* ............... START ............... */
public class JavaRightShiftOperator {
public static void main(String args[]) {
int x = -4;
System.out.println(x >> 1);
int y = 4;
System.out.println(y >> 1);
}
}
/* ............... END ............... */
Output
Notes:
-
In Java, the operator ‘>>’ is signed right shift operator. All integers are signed in Java, and it is fine to use >> for negative numbers. The operator ‘>>’ uses the sign bit (left most bit) to fill the trailing positions after shift. If the number is negative, then 1 is used as a filler and if the number is positive, then 0 is used as a filler. For example, if binary representation of number is 10….100, then right shifting it by 2 using >> will make it 11…….1.
Tags
Right Shift Operator, Java, Operators