Logical Binary Shifts
A logical binary shift slides every bit in a register left or right. It is the fastest way a processor can multiply or divide a positive whole number by 2, 4, 8, 16… It also has firm limits: bits that slide off the end are gone for good. You need to be able to perform a shift on an 8-bit positive integer, state the effect it has on the number, and say when that effect breaks down.
1. What a logical shift does
Three rules cover every logical shift question:
- Every bit moves the same number of places in the same direction. The pattern itself does not change; only its position does.
- The gap is filled with 0. A left shift feeds zeros in at the right-hand end (the least significant bit, LSB). A right shift feeds zeros in at the left-hand end (the most significant bit, MSB).
- Bits that fall off the end are discarded. The register is still exactly 8 bits wide after the shift, so whatever does not fit is thrown away.
2. Logical left shift: multiply by 2
Each column in binary is worth twice the column to its right. Moving every bit one column to the left therefore doubles the value of every 1, and so doubles the whole number. Here the bit that fell off the left-hand end was a 0, so nothing of value was lost and the answer is exactly right.
3. Logical right shift: divide by 2
Moving every bit one column to the right halves the value of each 1. For an even number the answer is exact. For an odd number the LSB is a 1, worth ½ after the shift, and there is no column to hold it. It is discarded, so the result is the answer rounded down to a whole number. This is integer division: 183 ÷ 2 gives 91, and the remainder 1 is thrown away.
4. Shifting more than one place
Each place doubles or halves the number once, so a shift of n places multiplies or divides by 2 a total of n times.
| Places shifted | Left shift multiplies by | Right shift divides by |
|---|---|---|
| 1 | 2 | 2 |
| 2 | 4 (2 × 2) | 4 |
| 3 | 8 (2 × 2 × 2) | 8 |
| 4 | 16 | 16 |
| n | 2n | 2n, remainder discarded |
The tables below follow one number through several shifts. Watch the last column: the effect holds until a 1 drops off the end, and then it breaks.
0000 1101 (13) shifted left
| Places | Result | Denary | Correct? |
|---|---|---|---|
| 1 | 0001 1010 | 26 | 13 × 2 |
| 2 | 0011 0100 | 52 | 13 × 4 |
| 3 | 0110 1000 | 104 | 13 × 8 |
| 4 | 1101 0000 | 208 | 13 × 16 |
| 5 | 1010 0000 | 160 | should be 416 |
1011 0110 (182) shifted right
| Places | Result | Denary | Exact answer |
|---|---|---|---|
| 1 | 0101 1011 | 91 | 91 |
| 2 | 0010 1101 | 45 | 45.5 |
| 3 | 0001 0110 | 22 | 22.75 |
| 4 | 0000 1011 | 11 | 11.375 |
5. The limitations of logical shifting
A shift is only a reliable multiply or divide while no 1s are lost. These are the limits the exam expects you to recognise.
1. Left shifts can overflow
If a 1 is shifted out of the MSB, the answer is too big for 8 bits and the result is wrong. Above, 13 shifted left 5 places should be 416, which needs 9 bits, so 160 is stored instead.
A left shift of n places is only safe if the n leftmost bits are all 0.
2. Right shifts lose precision
If a 1 is shifted out of the LSB, a fraction is thrown away and the result is rounded down. 0001 0111 (23) shifted right 2 places gives 0000 0101 (5), not 5.75.
The result is only exact if the n rightmost bits are all 0.
3. Lost bits cannot be recovered
A shift cannot simply be undone by shifting back. The lost bit is replaced by a 0, not restored:
1100 0011 (195) → left 1 → 1000 0110 → right 1 → 0100 0011 (67)
4. Powers of 2 only
A shift can only multiply or divide by 2, 4, 8, 16… Multiplying by 3, 5 or 10 cannot be done with a single shift. A processor would combine shifts with addition instead: x × 10 is (x shifted left 3) + (x shifted left 1).
5. Positive integers only
A logical right shift always puts a 0 into the MSB. In two’s complement the MSB is the sign bit, so a negative number becomes positive. 1111 1000 (−8) shifted right 1 gives 0111 1100 (+124), not −4. See the next lesson for two’s complement.
6. Shift too far and nothing is left
Shifting an 8-bit number 8 or more places in either direction pushes every original bit out, leaving 0000 0000 whatever the starting value.
6. Why computers use shifts
- Speed. A shift is one of the simplest things a processor can do, usually far quicker than a general multiply or divide. Compilers routinely replace “× 8” with “shift left 3”.
- Moving bits into position. Shifts line a group of bits up with the place they need to go, for example when packing several small values into one byte or when reading sensor data one bit at a time.
7. Check yourself
Perform a logical left shift of 2 places on 0011 0101. Give the denary value before and after, and state the effect.
Result: 1101 0100. Before: 53. After: 212. The number has been multiplied by 4 (22), and 53 × 4 = 212 exactly because the two bits shifted out of the MSB were both 0.
Perform a logical right shift of 3 places on 1100 1010. Is the result an exact division?
Result: 0001 1001 = 25. The original was 202, and 202 ÷ 8 = 25.25. The result is not exact: the bits 010 shifted out of the LSB end included a 1, so the remainder (0.25) was lost and the answer was rounded down.
1001 0000 (144) is shifted left by 1 place. Explain why the result is not 288.
The result is 0010 0000 = 32. The MSB was a 1, which was shifted out of the 8-bit register and lost. 288 needs 9 bits and cannot be stored in 8, so the shift produced an overflow and an incorrect result.
State the type of shift, and the number of places, that divides a positive binary integer by 16.
A logical right shift of 4 places, because 24 = 16. Any remainder is discarded.
0110 1101 is shifted right 1 place and then left 1 place. Explain why the final value is not the original value. [2]
The right shift gives 0011 0110 (54) and the left shift then gives 0110 1100 (108), not 109. The LSB of the original was a 1, and it was lost during the right shift. The left shift feeds in a 0 in its place, so the lost bit cannot be recovered.
Give two limitations of using logical shifts to perform multiplication and division. [2]
Any two of: a left shift causes an overflow and a wrong result if a 1 is shifted out of the MSB; a right shift loses the remainder (rounds down) if a 1 is shifted out of the LSB; shifts can only multiply or divide by powers of 2; logical shifts are only correct for positive integers, because a right shift puts 0 into the sign bit of a negative two’s complement number.