Shift Operations - Left, Right or Unsigned Right

This section describes shift operations: 'Left Shift', 'Right Shift', and 'Unsigned Right Shift'. A tutorial example is provided to show you how shift operations work on 'int' data type values.

What is a shift operation? A shift operation is an operation that requires the operand to be represented in a binary format, viewed as a bit string, then shift all bit values to the left or right.

Again, Java defines shift operations on "int" data type values, not on "byte" data type values. So a shift operation requires that the involved "int" value to be represented into a bit string of 32 bits. And the operation will be applied to shift bit values to the left or right on the bit string.

Java defines 3 shift operations called: "Left Shift", "Right Shift", and "Unsigned Right Shift":

1. Shift operation "Left Shift" - "<<": The "Left Shift" operation on an "int" value can be described as:

• Representing the first operand, an "int" values, into a bit string of 32 bits.
• Shift all bit values to the left and fill the empty bits on the right end with a 0 value. The number of bits to shift is given in the second operand.
• Converting the shifted bit string to an "int" value as the operation result.

The following diagram gives an example of a "<<" operation in 2 formats:

```  Decimal             Binary
-858993460  << 6     11001100110011001100110011001100 << 6
-----------------   ---------------------------------------
= 858993408         = 00110011001100110011001100000000
```

2. Shift operation "Right Shift" - ">>": The "Right Shift" operation on an "int" value can be described as:

• Representing the first operand, an "int" values, into a bit string of 32 bits.
• Shift all bit values to the right and fill the empty bits on the left end with the original value of the sign bit, the most left bit of the bit string. The number of bits to shift is given in the second operand.
• Converting the shifted bit string to an "int" value as the operation result.

The following diagram gives an example of a ">>" operation in 2 formats:

```  Decimal             Binary
-858993460  >> 6     11001100110011001100110011001100 >> 6
-----------------   ---------------------------------------
=4281545523         = 11111111001100110011001100110011
```

3. Shift operation "Unsigned Right Shift" - ">>>": The "Unsigned Right Shift" operation on an "int" value can be described as:

• Representing the first operand, an "int" values, into a bit string of 32 bits.
• Shift all bit values to the right and fill the empty bits on the left end with a 0 value. The number of bits to shift is given in the second operand.
• Converting the shifted bit string to an "int" value as the operation result.

The following diagram gives an example of a ">>>" operation in 2 formats:

```  Decimal             Binary
-858993460  >>> 6     11001100110011001100110011001100 >>> 6
-----------------   ---------------------------------------
=  53687091         = 00000011001100110011001100110011
```

To verify the examples given above, I wrote the following test program, ShiftOperations.java:

```/* ShiftOperations.java
*/
public class ShiftOperations {
public static void main(String[] arg) {
int a = -858993460; // binary: 11001100110011001100110011001100

int o1 = a << 6;  // left shift
int o2 = a >> 6;  // right shift
int o3 = a >>> 6; // unsigned right shift

System.out.println(" a: "+getBitString(a) +" = "+a);
System.out.println("o1: "+getBitString(o1) +" = "+o1);
System.out.println("o2: "+getBitString(o2) +" = "+o2);
System.out.println("o3: "+getBitString(o3) +" = "+o3);
}
private static String getBitString(int x) {
StringBuffer buf = new StringBuffer();
for (int i=1; i<=32; i++) buf.append(x>>>(32-i) & 0x00000001);
return buf.toString();
}
}
```

The output of the test program matches my earlier examples:

``` a: 11001100110011001100110011001100 = -858993460
o1: 00110011001100110011001100000000 = 858993408
o2: 11111111001100110011001100110011 = -13421773
o3: 00000011001100110011001100110011 = 53687091
```