Logical and arithmetic shifts (Edexcel GCSE Computer Science): Revision Notes
Logical and arithmetic shifts
What are bit shifts?
Binary bit patterns can be manipulated by shifting their contents in different directions. Think of it like moving all the digits in a binary number either left or right by a certain number of positions. There are simple rules that govern how these shifts work, and understanding them is essential for working with binary data.
Bit shifts are commonly used in computer programming for efficient multiplication and division, as well as for manipulating individual bits within data.
Logical shift left
When you perform a logical shift left, you're moving every binary digit a specified number of positions to the left.
The process for logical shift left:
- Move each binary digit n positions to the left
- Discard any leftmost bits that get pushed off the edge
- Fill the empty spaces on the right with 0s
Worked Example: Logical Shift Left
Let's shift the bit pattern 0001 0100 left by 2 places.
Starting with: 0 0 0 1 0 1 0 0
After shifting left by 2: 0 1 0 1 0 0 0 0
Each digit moves 2 places to the left. The two leftmost bits (0 and 0) are discarded, and the two empty spaces on the right are filled with 0s.
Logical shift right
Logical shift right works in the opposite direction, moving all binary digits to the right.
The process for logical shift right:
- Shift each binary digit n positions to the right
- Discard any rightmost bits that fall off the edge
- Fill the empty spaces on the left with 0s
Worked Example: Logical Shift Right
Let's shift the bit pattern 1011 1000 right by 3 places.
Starting with: 1 0 1 1 1 0 0 0
After shifting right by 3: 0 0 0 1 0 1 1 1
Each digit moves 3 places to the right. The three rightmost bits (0, 0, 0) are discarded, and the three empty spaces on the left are filled with 0s.
Arithmetic shift right
Arithmetic shift right is almost identical to logical shift right, but with one crucial difference: instead of filling the vacant spaces on the left with 0s, we fill them with the value of the original Most Significant Bit (MSB).
The MSB is the leftmost bit in a binary pattern, and in signed binary numbers, it represents the sign (positive or negative).
Worked Example: Arithmetic Shift Right
Let's shift the bit pattern 1110 1011 right by 3 places using arithmetic shift.
Starting with: 1 1 1 0 1 0 1 1
After arithmetic shift right by 3: 1 1 1 1 1 1 0 1
The MSB was 1, so the three vacant spaces on the left are filled with 1s instead of 0s.
An arithmetic shift left works exactly the same as a logical shift left, because the MSB gets shifted out of the pattern anyway. The vacant positions on the right are always filled with 0s.
Precision of numbers
When we shift bit patterns that represent actual numbers, we can lose precision and get unexpected results.
Example of precision loss:
- The bit pattern 0000 0100 (which represents 4) shifted left by 1 place becomes 0000 1000 (which represents 8). This makes sense - we've effectively multiplied by 2.
- However, the bit pattern 1000 0100 (which represents -124 in signed binary) shifted left by 1 place becomes 0000 1000 (which represents 8).
Another example:
- The bit pattern 0000 0100 (4) shifted right by 1 place becomes 0000 0010 (2). This makes sense - we've divided by 2.
- But the bit pattern 0000 0111 (7) shifted right by 1 place becomes 0000 0011 (3), not 3.5.
The key point is that shifting can cause loss of information, especially when dealing with numbers that don't divide evenly or when dealing with signed numbers.
Practice exercises
Try these examples to test your understanding:
Practice Exercise 1: Logical shift left, 2 places, applied to 0001 0100:
- Move each bit 2 positions left
- Discard leftmost bits that fall off
- Fill right side with 0s
- Answer: 0101 0000
Practice Exercise 2: Logical shift right, 3 places, applied to 1011 1000:
- Move each bit 3 positions right
- Discard rightmost bits that fall off
- Fill left side with 0s
- Answer: 0001 0111
Practice Exercise 3: Arithmetic shift right, 1 place, applied to 1000 1000:
- Move each bit 1 position right
- Discard rightmost bit that falls off
- Fill left side with the original MSB (1)
- Answer: 1100 0100
Remember!
Key Points to Remember:
- Logical shifts always fill empty spaces with 0s, regardless of direction
- Arithmetic shift right preserves the sign by filling with the Most Significant Bit (MSB)
- Arithmetic shift left works the same as logical shift left
- Shifting can cause precision loss when working with actual numbers
- Left shifts generally multiply by powers of 2, while right shifts divide by powers of 2