Right shift java: In the previous article we have discussed about Java Program on Bitwise Left Shift Operator
In this article we will see the use of Bitwise Right Shift operator in Java programming language.
Java Program on Bitwise Right Shift Operator
Java right shift: Bitwise Right Shift operator is also called as Signed Right Shift operator which is represented by >> symbol. It shifts the bits of a number towards right with specified position.
During shift the right most bit(least significant) is discarded and the left most bit(most significant) is replaced with sign bit.
Syntax:
value>>position
Where
valuerepresents the binary value on which shift operation will be performedpositionrefers to the specified position on which right shift will happen by shifting bits towards right with that position
For Example:
When the number is 8 whose binary is 1000 After 2 bit right shift, the value will become 0010 which is equivalent to 2 When the number is -8 whose binary is 1000, here signed bit is 1 After 2 bit right shift, the value will become 1110 which is equivalent to -2
Program-1:(4 bit right shift operation)
class Main
{
public static void main(String[] args)
{
//number is 256
int num = 256;
//performing 4 bit right shift operation
int result = num >> 4;
//prints 64
System.out.println("After right shift operation: "+result);
}
}
Output: After right shift operation: 16
Program-2:(2 bit right shift operation)
public class Main
{
public static void main(String args[])
{
//number declared
int x = 8;
int y= -8;
//performing 2 bit signed right shift operation
int resultX=x>>2;
int resultY=y>>2;
System.out.println("After signed right shift operation x: " + resultX);
System.out.println("After signed right shift operation y: " + resultY);
}
}
Output: After signed right shift operation x: 2 After signed right shift operation y: -2
Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Related Java Programs: