Shift: LeftShift And Rightshift Operator In JAVA With Example

Shift Operator is of two types Bitwise Left Shift And Right Shift operator. In the Bitwise shift operators, the result comes applying these operators on the bit values. Below we will discuss both the Shift operator.


BITWISE LEFT SHIFT OPERATOR

As you know very well, bit is a concept of binary number system. Here if you consider any number like 2 whose binary equivalent is 10 (i.e. one zero). And if on this particular number we apply left shift operator then it shift its value toward left as much number of bits as you defined and place zeros at the right most places. And this process goes on as much the size of that particular type is described.

Example, if you consider the int type data this will move bits upto number 31 places. Because it is having total number of 32 bits in java. And if you consider long type data then it will move upto 63 places.

To move the number of bits is described by special notation ‘<<’.

Important Note: Bitwise Left Shift is assigned in the format of val<<num, where val represents the number to whom we have to find left shift and num specifies the number of bits as we want to shift.

Bitwise Left Shift Operator Example Program:

From program point of view you can understand this very easily. Here we are considering a simple int data type having 2 value to which we want just one left shit. As number 2’s binary equivalent is 10 (i.e. one zero). And if its bits are simple move just one left shift then resultant value will be 100 (one zero zero) whose decimal is 4. And that’s the truth that when input given in program code available and output result is 4 which is desired.

In the code given here value to which we want to find the left shift operator is 2 and the number of places as we want to move is 1.

public class LeftShiftOperator {

	public static void main(String[] args) {
		 
				int a=2;//10
				int i;
				i=a<<1;//100
				System.out.println("the value of a before left shift is: " +a);
				System.out.println("the value of a after applying left shift is: " +i);
		
	}

}

Output:

the value of a before left shift is: 2
the value of a after applying left shift is: 4

BITWISE RIGHT SHIFT OPERATOR:

Bitwise Right Shift Operator is applied on the number of bits. In this shift operator the Bit value for given number is simply move to right side and left most bits are replaced with the zeros.

Important Note: This is simply assigned as val>>num, where val is the particular value whose right shift is to be calculated and num indicate the particular number of bits shift movement.

Bitwise Right Shift Operator Example Program:

For example, if you want to apply right shift operator on integer value 2 for one shift only. Then result you will get will be 1. Understand how this comes.

As you know simply binary number of 2 is 10 (i.e. one zero) and here we are applying a right shift operator for one position. So after the movement of one toward right side result will be 01  (i.e. zero one) which is is equal to 1 only.

Same result is calculated with the help of program. From code your intuition regarding this topic will be very clear. So just read out the program and then output for code is also given.

public class RightShiftOperator {

	public static void main(String[] args) {
		int a=2;
		int i;
		i=a>>1;
		System.out.println("the value of a before right shfit is: " +a);
		System.out.println("the value of a after applying right shfit is: " +i);


	}

}

Output:

the value of a before right shfit is: 2
the value of a after applying right shfit is: 1