//Logical Vs. Arithmetic Shift

Logical Vs. Arithmetic Shift

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.

By |2018-08-17T22:57:20+00:00December 13th, 2016|Categories: Explained Simply|Tags: , |7 Comments

7 Comments

  1. brad 18/10/2017 at 20:06 - Reply

    Hello! Thanks for the article, but the wording is unclear here:

    “Let’s take the decimal number 2 represented as 4 bit binary number 0010. By shifting in to the right 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.”

    Obviously a right shift will divide by 2^n, not multiply. I suggest you just say, By shifting left, we multiply

    • Yasen Stoyanov 21/10/2017 at 17:48

      Hello brad,
      Thanks for catching the mistake and providing us with a feedback. We will fix the wording in the article.

  2. skywood 06/06/2018 at 16:04 - Reply

    for left arithmetic shift ,i think the MSB should retain to avoid overflow

    • Yasen Stoyanov 06/06/2018 at 22:40

      Hello skywood,
      By definition the left shift operation discards the MSB and this indeed may cause overflow which is undesired in almost every digital system. The solution is to implement additional logic (functionality) that saturates the result of the shift operation if an overflow has occurred.
      The following article contains some basic concepts regarding overflow situations and how to detect them:
      https://open4tech.com/overflow-digital-systems/

  3. JudeJuicy 22/06/2018 at 21:36 - Reply

    I often visit your blog and have noticed that you don’t update it often. More frequent updates will give your
    site higher authority & rank in google. I know that writing articles takes a lot of time, but you can always help yourself with miftolo’s tools which will shorten the time of creating an article to a few seconds.

  4. Tama McGlinn 17/08/2018 at 12:03 - Reply

    The final 1-bit arithmetic right-shift doesn’t seem to actually divide by 2:
    1010 -> 1101

    If these are unsigned numbers (you didn’t specify), then 10 / 2 = 5, which is 0101, which would be the result of a logical 1-bit right-shift. If these are signed numbers, then -2 / 2 = -1, which is 1001.

    This is all assuming signed magnitude representation, which worked for understanding all the preceding examples. Perhaps you meant 2’s complement?
    1010 = -6, -6 / 2 = -3, which is 1101

    My conclusion is that you should mention that this is all in 2’s complement, since your first examples handle unsigned numbers. Also, add the interpreted values into the final example to make it clearer that the result is correct.

    • Yasen Stoyanov 18/08/2018 at 00:07

      Hello Tama,
      Thank you for the feedback!
      You are absolutely correct that it was not clear enough that we use 2’s complement in the last example. We have updated the article and it now incorporates your suggestion.

Leave A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.