1.1 Number systems

Show All Section Notes
Test yourself on Number systems 11 questions — drag-to-order, code completion, matching and multiple choice.
Start the quiz

Logical Binary Shifts

Watch this lesson Video 1.1.5 · 9:55 · Multiplying and dividing by moving bits — and exactly when it stops working

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

Definition A logical shift moves every bit in a binary number the same number of places to the left or to the right. Positions left empty are filled with 0. Any bit pushed past either end of the register is lost.

Three rules cover every logical shift question:

  1. Every bit moves the same number of places in the same direction. The pattern itself does not change; only its position does.
  2. 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).
  3. 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

LOGICAL LEFT SHIFT by 1 place 128 64 32 16 8 4 2 1 Before After 0 1 0 1 1 0 0 1 1 0 1 1 0 0 1 0 0 lost 0 0 fills the gap = 89 = 178 Every bit moves one place left and is now worth double: 89 × 2 = 178
Figure 1 — A left shift moves each bit into the next column up, so every 1 is worth twice as much as before.

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.

Shift 0010 0110 (38) left by 2 places 0010 0110 38 1001 1000 152 every bit moved 2 places left, two 0s fed in at the right effect: 38 x 2 x 2 = 38 x 4 = 152

3. Logical right shift: divide by 2

LOGICAL RIGHT SHIFT by 1 place 128 64 32 16 8 4 2 1 Before After 1 0 1 1 0 1 1 1 0 1 0 1 1 0 1 1 1 lost 0 0 fills the gap = 183 = 91 Every bit moves one place right and is now worth half: 183 ÷ 2 = 91.5, but only 91 can be stored
Figure 2 — A right shift halves the value of every 1. The 1 that fell off the right was worth ½, so the remainder is lost.

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 shiftedLeft shift multiplies byRight shift divides by
122
24  (2 × 2)4
38  (2 × 2 × 2)8
41616
n2n2n, 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
PlacesResultDenaryCorrect?
10001 10102613 × 2
20011 01005213 × 4
30110 100010413 × 8
41101 000020813 × 16
51010 0000160should be 416
1011 0110 (182) shifted right
PlacesResultDenaryExact answer
10101 10119191
20010 11014545.5
30001 01102222.75
40000 10111111.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.
Exam technique Always write the result as a full 8-bit number, padding with 0s. When asked for the effect, give the operation and the factor (“multiplied by 4”, not just “bigger”). If a 1 was shifted out, say so: that is the mark for the limitation. Name the bits correctly too: the MSB is on the left, the LSB is on the right.

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.