Difference between The Bitwise Operators >>, >>> and << in Java

Difference between The Bitwise Operators >>, >>> and << in Java

1 <<

Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. In a nutshell it add zeros the right side of the binary representation of the number.

int i = 6;
//print: The number is: 6
System.out.println("The number is: " + i);
//print: The binary representation is: 110 
System.out.println("The binary representation is: " + Integer.toBinaryString(i));

int t = i<<1;
//print: The number is: 12
System.out.println("The number is: " + t);
//print: The binary representation is: 1100
System.out.println("The binary representation is: " + Integer.toBinaryString(t));
t = i<<2;
//print: The number is: 24
System.out.println("The number is: " + t);
//print: The binary representation is: 11000
System.out.println("The binary representation is: " + Integer.toBinaryString(t));

i = Integer.MAX_VALUE;
//print: The number is: 2147483647
System.out.println("The number is: " + i);
//print: The binary representation is: 1111111111111111111111111111111 
System.out.println("The binary representation is: " + Integer.toBinaryString(i));
t = i<<1;
//print: The number is: -2
System.out.println("The number is: " + t);
//print: The binary representation is: 11111111111111111111111111111110
System.out.println("The binary representation is: " + Integer.toBinaryString(t));
  • For the max value 1111111111111111111111111111111 it's real representation is 01111111111111111111111111111111 (0 for the positive sign) so when you add a 0 to the left the result is 11111111111111111111111111111110 which is -2 in base 10.

2 >>

Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. In a nutshell it removes binaries from the right side of the binary representation of the number and fill the holes in the left with zeros without touching the sign bite.

int i = 6;
//print: The number is: 6
System.out.println("The number is: " + i);
//print: The binary representation is: 110 
System.out.println("The binary representation is: " + Integer.toBinaryString(i));

int t = i<<1;
//print: The number is: 3
System.out.println("The number is: " + t);
//print: The binary representation is: 11
System.out.println("The binary representation is: " + Integer.toBinaryString(t));
t = i<<2;
//print: The number is: 1
System.out.println("The number is: " + t);
//print: The binary representation is: 1
System.out.println("The binary representation is: " + Integer.toBinaryString(t));

i = Integer.MAX_VALUE;
//print: The number is: 2147483647
System.out.println("The number is: " + i);
//print: The binary representation is: 1111111111111111111111111111111 
System.out.println("The binary representation is: " + Integer.toBinaryString(i));
t = i<<1;
//print: The number is: 1073741823
System.out.println("The number is: " + t);
//print: The binary representation is: 111111111111111111111111111111
System.out.println("The binary representation is: " + Integer.toBinaryString(t));

i = -Integer.MAX_VALUE;
//print: The number is: -2147483647
System.out.println("The number is: " + i);
//print: The binary representation is: 10000000000000000000000000000001 
System.out.println("The binary representation is: " + Integer.toBinaryString(i));
t = i<<1;
//print: The number is: -1073741824
System.out.println("The number is: " + t);
//print: The binary representation is: 11000000000000000000000000000000
System.out.println("The binary representation is: " + Integer.toBinaryString(t));
  • It does not affect the sign of the value.

3 >>>

Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros. In a nutshell it removes binaries from the right side of the binary representation of the number like >>. The only difference that it will affect the sign bite if the value is negative and will be filled with 0 transforming the number to a positive value.

int i = 6;
//print: The number is: 6
System.out.println("The number is: " + i);
//print: The binary representation is: 110 
System.out.println("The binary representation is: " + Integer.toBinaryString(i));

int t = i<<1;
//print: The number is: 3
System.out.println("The number is: " + t);
//print: The binary representation is: 11
System.out.println("The binary representation is: " + Integer.toBinaryString(t));
t = i<<2;
//print: The number is: 1
System.out.println("The number is: " + t);
//print: The binary representation is: 1
System.out.println("The binary representation is: " + Integer.toBinaryString(t));

i = Integer.MAX_VALUE;
//print: The number is: 2147483647
System.out.println("The number is: " + i);
//print: The binary representation is: 1111111111111111111111111111111 
System.out.println("The binary representation is: " + Integer.toBinaryString(i));
t = i<<1;
//print: The number is: 1073741823
System.out.println("The number is: " + t);
//print: The binary representation is: 111111111111111111111111111111
System.out.println("The binary representation is: " + Integer.toBinaryString(t));

i = -Integer.MAX_VALUE;
//print: The number is: -2147483647
System.out.println("The number is: " + i);
//print: The binary representation is: 10000000000000000000000000000001 
System.out.println("The binary representation is: " + Integer.toBinaryString(i));
t = i<<1;
//print: The number is: 1073741824
System.out.println("The number is: " + t);
//print: The binary representation is: 1000000000000000000000000000000
System.out.println("The binary representation is: " + Integer.toBinaryString(t));
  • It does affect the sign of the value returning a positive value.