A-Level Computer Science / Unit 4: CPU Operation and Low-Level Processing

4.3.1 Binary Shifts

πŸ”’ Lesson slides are available to signed-in users. Sign in

Binary Shifts

A binary shift changes the positions of bits in a register. The register width stays fixed, so each shift type has a rule for the bit that enters an empty position and for any bit that leaves the register.

By the end of this page, you should be able to:

  • perform logical, arithmetic and cyclic shifts left or right;
  • state which bits are lost, introduced, copied or wrapped around;
  • compare logical and arithmetic right shifts using the sign bit;
  • keep the result at the original register width.

1. Keep the register width fixed

A shift moves bits left or right by a stated number of places. Bits do not change into a wider or narrower register. Any result must have the same number of bits as the starting value.

When a bit moves beyond one end of the register, what happens to it depends on the shift type. For a shift of more than one place, repeat the rule one place at a time.

Do not add extra register positions

Keep only the original register width. A bit shifted past the edge may be lost or may wrap around, depending on the shift type.

2. Logical shifts move bits and fill with zero

In a logical shift, every bit moves in the stated direction. A zero enters each vacant position. Any bit shifted beyond the register width is lost.

OperationBit entering the registerBit leaving the register
Logical shift left (LSL)Zero enters at the right.The leftmost bit is lost.
Logical shift right (LSR)Zero enters at the left.The rightmost bit is lost.

For a shift of several places, apply the same movement repeatedly. Keep the result at the fixed width after each movement and note the outgoing bits that are lost.

3. Arithmetic shifts work with signed values

An arithmetic shift is used with a signed binary value. In an arithmetic right shift, the sign bit is copied into each new leftmost position. This helps keep the sign of the value.

In an arithmetic left shift, bits move left and zero enters on the right. The bit leaving the left end is lost. If this changes the sign of a signed value, the result may overflow the register's range.

OperationWhat entersWhat to watch
Arithmetic rightA copy of the sign bit enters on the left.The rightmost outgoing bit is lost.
Arithmetic leftZero enters on the right.The leftmost outgoing bit is lost; signed overflow may occur.
When comparing right shifts, check the leftmost bit: logical right shift inserts zero; arithmetic right shift copies the sign bit.

4. Cyclic shifts rotate the bits

A cyclic shift rotates the bit pattern. The bit that leaves one end re-enters at the other end. No bit is lost and no new zero replaces it.

OperationOutgoing bitWhere it goes
Cyclic leftThe leftmost bitIt wraps to the rightmost position.
Cyclic rightThe rightmost bitIt wraps to the leftmost position.
Rotate, do not discard: a cyclic shift keeps every bit and maintains the same register width.

5. Compare the three shift types

Shift typeWhat fills the open position?What happens at the edge?
LogicalZeroThe outgoing bit is lost.
Arithmetic rightA copy of the sign bit on the leftThe rightmost outgoing bit is lost.
Arithmetic leftZero on the rightThe leftmost outgoing bit is lost; signed overflow may occur.
CyclicThe outgoing bit from the other endThe bit wraps around; none is lost.
In an explanation, state three things: the direction, the type of shift and what happens to the bit entering or leaving the register.

Interactive: shift a fixed-width register

Choose the shift type, direction and number of places. Then move one place at a time. This new eight-bit example lets you see the outgoing bit and the bit inserted or wrapped at each step.

FIXED-WIDTH SHIFT LAB

Choose a shift operation

Ready

Starting register 10000011 8 bits Β· fixed width
Shift type
Direction
Places
Current registerBefore the shift
Outgoing bitβ€”
Incoming bitβ€”

Shift rule

Select a shift operation, then use Next shift to move one place.

The register always stays eight bits wide. Logical and arithmetic shifts can lose an outgoing bit; a cyclic shift wraps it around.

6. Avoid these common mix-ups

Mix-upCorrect explanation
Using zero for every right shiftArithmetic right shift copies the sign bit into the leftmost position.
Keeping a bit that has moved beyond the registerA logical or arithmetic shift loses the outgoing bit.
Discarding the bit in a cyclic shiftA cyclic shift wraps the outgoing bit to the opposite end.
Changing the register widthKeep the result at the original width after every shift.
Calling the sign bit a new zeroFor an arithmetic right shift, copy the original sign bit into each new leftmost position.

7. Check your understanding

Try each question before opening the suggested response.

  1. In a logical left shift, what enters at the right and what happens to the leftmost bit?
    Suggested response

    Zero enters at the right. The leftmost bit is shifted out and lost.

  2. How does an arithmetic right shift differ from a logical right shift?
    Suggested response

    A logical right shift inserts zero at the left. An arithmetic right shift copies the sign bit into the leftmost position.

  3. What happens to the rightmost bit in a cyclic right shift?
    Suggested response

    It wraps around and enters at the leftmost position. No bit is lost.

  4. Why must a shifted result keep the original register width?
    Suggested response

    A shift changes the positions of bits within the register; it does not change the register's number of bit positions.

  5. What risk can occur after an arithmetic left shift of a signed value?
    Suggested response

    A bit can be lost at the left edge and the sign may change, so the signed result may overflow the range represented by the register.

Final check

Show a result at the stated width and explain the edge bits. For a right-shift comparison, identify whether zero or the sign bit enters at the left. For a cyclic shift, state which bit wraps around.