Logical Shift and Arithmetic Shift are bit manipulation operations (bitwise operations).
- 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.
- 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
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.
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.
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.
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
Thanks for catching the mistake and providing us with a feedback. We will fix the wording in the article.
for left arithmetic shift ,i think the MSB should retain to avoid overflow
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:
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.
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.
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.
hello thank you a lot for this blog. but i have question
-18 is 11101110 in 2s complement. what i will get if i use Arithmetic shift left by 2 bits for this?
When you shift 11101110 (-18 in 2’s complement) to the left by two positions you will get 10111000 (-72 in 2’s complement). Keep in mind that one additional left shift of this 8bit binary number 10111000 (-72) will cause an overflow and you should take precautions :)
Can i get overflow when i use logical shift left or logical shift right or arithmetic shift right ?
Yes, if you can get overflow when doing left shift (arithmetic and logic)
Non-equivalence of arithmetic right shift and division
However, arithmetic right shifts are major traps for the unwary, specifically in treating rounding of negative integers. For example, in the usual two’s complement representation of negative integers, −1 is represented as all 1’s. For an 8-bit signed integer this is 1111 1111. An arithmetic right-shift by 1 (or 2, 3, …, 7) yields 1111 1111 again, which is still −1. This corresponds to rounding down (towards negative infinity), but is not the usual convention for division.
It is frequently stated that arithmetic right shifts are equivalent to division by a (positive, integral) power of the radix (e.g., a division by a power of 2 for binary numbers), and hence that division by a power of the radix can be optimized by implementing it as an arithmetic right shift. (A shifter is much simpler than a divider. On most processors, shift instructions will execute faster than division instructions.) Large number of 1960s and 1970s programming handbooks, manuals, and other specifications from companies and institutions such as DEC, IBM, Data General, and ANSI make such incorrect statements [page needed].
I don’t get it why the definition of arithmetic shift left doesn’t keep the sign number to prevent the overflow just like the arithmetic right shift? What’s the logic here? Could you please explain?