Bitwise right shift operators are important operations in computer programming languages like Java and C/C++. In C/C++, there is only one right shift operator '>>' which is recommended to use only for positive integers or unsigned integers. Using the right shift operator for negative numbers is not recommended in C/C++, and the output can be compiler dependent. On the other hand, Java supports two right shift operators - signed right shift ">>" and unsigned right shift ">>>".
Signed Right Shift (>>) Operator: In Java, the operator '>>' is a signed right shift operator. All integers are signed in Java, and it is perfectly fine to use >> for negative numbers. The operator '>>' uses the sign bit (leftmost bit) to fill the trailing positions after the 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 the binary representation of a number is 10....100, then right shifting it by 2 using >> will make it 11.......1.
Let's take an example to understand this concept better.
Example:
csharp// Java Program to Illustrate Signed Right Shift Operator
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
int x = -4;
System.out.println(x >> 1);
int y = 4;
System.out.println(y >> 1);
}
}
Output: -2 2
In the above example, the variable x is assigned a value of -4, and variable y is assigned a value of 4. Then, the value of x and y are right shifted by 1 using the signed right shift operator '>>'. As x is a negative number, the output is -2, and as y is a positive number, the output is 2.
Unsigned Right Shift (>>>) Operator: In Java, the operator '>>>' denotes the unsigned right shift operator and always fills 0 irrespective of the sign of the number. Let's take an example to understand this concept better.
Example:
csharp// Java Program to Illustrate Unsigned Right Shift Operator
// Main class
class GFG {
// main driver method
public static void main(String args[])
{
// x is stored using 32 bit 2's complement form.
// Binary representation of -1 is all 1s (111..1)
int x = -1;
// The value of 'x>>>29' is 00...0111
System.out.println(x >>> 29);
// The value of 'x>>>30' is 00...0011
System.out.println(x >>> 30);
// The value of 'x>>>31' is 00...0001
System.out.println(x >>> 31);
}
}
Output: 7 3 1
In the above example, the variable x is assigned a value of -1, which is represented in binary as all 1s (111..1). Then, the value of x is right shifted by 29, 30, and 31 bits using the unsigned right shift operator '>>>'. As the unsigned right shift operator always fills 0 irrespective of the sign of the number, the output is 7, 3, and 1, respectively.
In conclusion, the bitwise right shift operators in Java are powerful tools that allow developers to manipulate the binary representation of numbers efficiently. By understanding the concepts and examples discussed above, you can use these operators to perform a wide range of operations in your Java programs.