4.3.1 Binary Shifts and Their Effects
A binary shift moves every bit in a fixed-width bit pattern. The result depends on the direction of movement and on what happens at the two ends of the register. This section compares logical, arithmetic and cyclic shifts in both directions.
By the end of this section, you should be able to:
- Perform left and right shifts on fixed-width binary values.
- Distinguish logical, arithmetic and cyclic shifts.
- Identify the bit that leaves the register and the bit introduced at the other end.
- Explain why an arithmetic right shift preserves the sign of a two's-complement value.
- Explain how cyclic shifts retain all original bits.
- Use
LSL #nandLSR #nwith the accumulator. - Relate shifts to multiplication or division by powers of two when the interpretation permits it.
- Recognise overflow, truncation and information loss caused by shifts.
Where This Page Fits
| Page | Main focus |
|---|---|
| 4.3.1 Binary Shifts and Their Effects | Logical, arithmetic and cyclic movement of bits. |
| 4.3.2 Bitwise Logic and Masks | AND, OR, XOR, testing bits and changing selected bits. |
| 4.3.3 Using Bits to Read and Control Devices | Applying flags and masks to sensor inputs and actuator outputs. |
How to Read a Shift Diagram
Every example on this page uses an 8-bit register. Bit 7 is the most significant bit and bit 0 is the least significant bit.
For each shift, ask three questions:
- Which direction do the existing bits move?
- Which bit leaves the fixed-width register?
- What enters the newly vacant position?
Logical Shifts: Vacated Positions Receive Zero
Logical left shift
00110110
Unsigned 54
01101100
Unsigned 108
Every bit moves one place left. The original bit 7 leaves the register and zero enters bit 0. Here no significant 1 is lost, so the unsigned value doubles.
Logical right shift
10110101
Unsigned 181
01011010
Unsigned 90
Every bit moves one place right. The original bit 0 is lost and zero enters bit 7. For an unsigned integer, this produces integer division by two. The lost 1 represents the discarded remainder.
Logical Shift Instructions: LSL and LSR
In the syllabus teaching instruction set, logical shifts operate on the accumulator:
| Instruction | Effect on ACC | Bit introduced |
|---|---|---|
LSL #n |
Shift the bits in ACC left by n places. | Zero enters from the right. |
LSR #n |
Shift the bits in ACC right by n places. | Zero enters from the left. |
ACC = 00010111
LSL #2
ACC = 01011100
The instruction above is equivalent to two one-place logical left shifts:
00010111
00101110
01011100
Arithmetic Shifts: Preserve Signed Meaning Where Possible
Arithmetic shifts are intended for signed two's-complement values. The most important difference appears during a right shift.
Arithmetic right shift of a positive value
00110110
+54
00011011
+27
Arithmetic right shift of a negative value
11010100
−44 in 8-bit two's complement
11101010
−22
The leftmost 1 is copied because it is the sign bit. Inserting zero instead would turn the result into a positive number and destroy the signed interpretation.
Arithmetic left shift
An arithmetic left shift usually moves the bit pattern in the same way as a logical left shift: bits move left and zero enters from the right. The pattern is interpreted as signed, so the result is only valid when the required signed value still fits.
11101010
−22
11010100
−44
01011100 left gives
10111000. The original value is +92, but +184 cannot be represented in
8-bit two's complement. The new pattern would be interpreted as a negative number,
so signed overflow has occurred.
Cyclic Shifts: The Leaving Bit Returns at the Other End
Cyclic left
10110001
01100011
The original leftmost 1 wraps around into bit 0.
Cyclic right
10110001
11011000
The original rightmost 1 wraps around into bit 7.
Shifting More Than One Place
A shift by n places is equivalent to repeating a one-place shift n times. The rule is applied again at every stage.
| Step | Logical right shift | Arithmetic right shift | Cyclic right shift |
|---|---|---|---|
| Start | 11001011 |
11001011 |
11001011 |
| After 1 place | 01100101 |
11100101 |
11100101 |
| After 2 places | 00110010 |
11110010 |
11110010 |
The arithmetic and cyclic results happen to match in this particular two-step example, but the processes are different: arithmetic right copies the current sign bit, while cyclic right wraps the outgoing bit.
How Shifts Affect Numerical Values
| Shift | Typical numerical effect | Condition |
|---|---|---|
| Logical left by n | Unsigned value × 2n | Only if no significant 1 is shifted out. |
| Logical right by n | Unsigned integer value ÷ 2n | Any discarded bits form a lost remainder. |
| Arithmetic right by n | Signed value divided by 2n | The sign is preserved; negative odd values require attention to rounding. |
| Arithmetic left by n | Signed value × 2n | Only if the signed result remains representable. |
| Cyclic left or right | No general multiply/divide rule | Rotation preserves the bit count, not the numerical magnitude. |
What Happens to a Bit That Leaves?
| Shift type | Outgoing bit | Incoming bit |
|---|---|---|
| Logical | Discarded from the stored value. | 0 |
| Arithmetic right | Discarded from the stored value. | Copy of the sign bit |
| Arithmetic left | Discarded; may reveal signed overflow. | 0 |
| Cyclic | Wrapped to the opposite end. | The same outgoing bit |
Some processor designs also copy the outgoing bit into a carry flag. Only use that behaviour when it is stated or when the processor model defines it.
Interactive: Binary Shift Visualiser
Enter an 8-bit pattern, choose the shift type and direction, and select how many places to move. Step through the shift one place at a time to see the outgoing and incoming bits.
Common Mistakes and Misconceptions
- Moving bits in the wrong direction because the instruction says “left” but the written arrow was misread.
- Changing only one bit instead of moving the complete bit pattern.
- Inserting 1 during a logical shift. Logical shifts insert zero.
- Using a logical right shift on a negative two's-complement value and claiming the sign is preserved.
- Forgetting to wrap the outgoing bit during a cyclic shift.
- Assuming an arithmetic left shift cannot overflow.
- Applying the multiply/divide rule without checking for lost significant bits.
- Treating a shift by three places as one unexplained jump rather than three repeated one-place operations.
Exam Tips
Label the shift before calculating
Write the type, direction and number of places, for example: arithmetic right by two.
Show the end rule
State what enters the vacant position: zero, the sign bit, or the outgoing wrapped bit.
Keep the width fixed
An 8-bit result must still contain exactly eight bits. Do not add a ninth bit to keep a value that should have left the register.
Practice
Perform the shifts
- Logically shift
01011001left by one place. - Logically shift
11100110right by two places. - Arithmetically shift
10110100right by one place. - Cyclically shift
01101001left by three places. - Cyclically shift
10010110right by two places.
Explain the effect
- Why does arithmetic right preserve the leftmost bit?
- Why is
LSR #1unsuitable for halving a negative two's-complement value? - Under what condition does
LSL #2multiply an unsigned value by four correctly? - Explain why a cyclic shift cannot generally be described as multiplication or division.
- How can an arithmetic left shift produce a negative-looking result from a positive input?
Assembly-language application
ACC contains 00011101. Show ACC after each independent instruction:
LSL #1, LSL #3, LSR #1 and
LSR #2.
Review
| Shift | What enters? | What happens to the outgoing bit? |
|---|---|---|
| Logical left/right | 0 | Discarded |
| Arithmetic right | Copy of sign bit | Discarded |
| Arithmetic left | 0 | Discarded; overflow may occur |
| Cyclic left/right | Outgoing bit from the other end | Wrapped around |