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:
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: