Logical Shift and Arithmetic Shift are bit manipulation operations (bitwise operations).

Logical Shift

  • A Left Logical Shift of one position moves each bit to the left by one. The vacant least significant bit (LSB) is filled with zero and the most significant bit (MSB) is discarded.
  • A Right Logical Shift of one position moves each bit to the right by one. The least significant bit is discarded and the vacant MSB is filled with zero.
Fig. 1 Logical Shift by one bit

Fig. 1 Logical Shift by one bit

Arithmetic Shift

  • A Left Arithmetic Shift of one position moves each bit to the left by one. The vacant least significant bit (LSB) is filled with zero and the most significant bit (MSB) is discarded. It is identical to Left Logical Shift.
  • A Right Arithmetic Shift of one position moves each bit to the right by one. The least significant bit is discarded and the vacant MSB is filled with the value of the previous (now shifted one position to the right) MSB.

Fig. 1 Left and Right Arithmetic Shift by One Bit

Fig. 1 Left and Right Arithmetic Shift by One Bit

Arithmetic Shift operations can be used for dividing or multiplying an integer variable.

Multiplication by left shift:

The result of a Left Shift operation is a multiplication by 2n , where n is the number of shifted bit positions.

Example:

Let’s take the decimal number 2 represented as 4 bit binary number 0010. By shifting in to the left with one position we get 0100 which is 4 in decimal representation. If we shift it once more we get binary value 1000 which is 8 in decimal representation.

For unsigned representation, when the first “1” is shifted out of the left edge, the operation has overflowed. The result of the multiplication is larger than the largest possible.

Shifting left on signed values also works, but overflow occurs when the most significant bit changes values (from 0 to 1, or 1 to 0).

Division by right shift:

The result of a Right Shift operation is a division by 2n , where n is the number of shifted bit positions.

Example:

If we have the binary number 01110101 (117 decimal) and we perform arithmetic right shift by 1 bit we get the binary number 00111010 (58 decimal).  So we have divided the original number by 2.

If we have the binary number 1010 (-6 decimal) and we perform arithmetic right shift by 1 bit we get the binary number 1101 (-3 decimal). So we have divided the original negative number by 2.

Note: The examples above use two’s complement representation.

Was this article helpful?