Shift Operator in java || Left shift << || Right shift >>


Shift Operator in java


what is shift operator in java


This operator used the shift(move) bit of the number left to right. In another word, the value of datatype multiplies and divide the number of two(2).

There are three types of Shift Operator in java 
 
No Operator USD Description
1     <<  A<<B Left shift 
2     >> A>>B Right shift 
3     >>> A>>>B Shift right zero fill 

right shift operator in java


Right shift operator shifts the bit of the number right side also work do like if the variable value is a so the output obtain like as the value of 

a/(2*1) = answer 

here (1) is print the value of "System.out.println" section like as (a>>1)
& The number (2) takes the machine code when the program run refers the example for the batter understand.

Example
public class RightShiftOperator {
	public static void main(String[] args) {
		
		int x=10;
		int y=20;
		int z=30;
		
		System.out.println(x>>1);
		// how to show the output :- 10/(2*1)=5
		System.out.println(y>>2);
		// second output :- 20/(2*2) = 5
     	        System.out.println(z>>2);
     	        // Third output :-  30/(2*2) = 7
     	
 }
}
Output
5
5
7


Left shift operator in java


Left shift operator shifts the bit of the number left side also, work do like if the variable value is a so the output obtain like as the value of 

a*(2^1) = answer 

here (1) is print the value of "System.out.println" section like as (a<<1) means the squere of the value 2 one time if the value of 2(a<<2) means the squre of the value like as (2*2) then after multiplication with the variable value

& The number (2) take the machine code when the program run show the example for the batter understand

Example

public class LeftShiftOperator {
	public static void main(String[] args) {
		
		int x=10;
		int y=20;
		
		System.out.println(x<<2);
		// how to show the output :- 10*((2^2)=(2*2))= 40
		System.out.println(y<<3);
		// second output :- 20*((2^3)=(2*2*2)) = 160
     	
 }
}
Output
40
160


unsigned right shift operator in java


Unsigned Right shift operator shifts the bit of the number right side also this variable value use only in the negative number value if the value of positive so work like right shift operator.

Example

public class UnsignedShiftOperator {
	public static void main(String[] args) {
		
		int x= -10;
		int y= -20;
		
		System.out.println(x>>>2);
		System.out.println(y>>>3);
     	
 }
}
Output
1073741821
536870909

Post a Comment

Previous Post Next Post