# Logical Binary Shifts

- **Track:** Cambridge IGCSE™ Computer Science (0478 / 0984) (CAMBRIDGE IGCSE 0478)
- **Module:** Section 1: Data Representation
- **Estimated Duration:** 15 minutes
- **Access:** Open Theory + Free Practice

---

## Theory & Conceptual Background

A **logical binary shift** moves all bits in a binary register a specified number of positions to the left or right. Vacated bit positions are always filled with zeros ($0$).

---

## Learning Objectives

- Perform logical binary left shifts and right shifts on positive 8-bit integers.
- Determine the mathematical effect of logical shifts (multiplication and integer division by powers of 2).
- Identify precision loss and overflow resulting from bit shifts.

---

## Core Syllabus Theory

### 1. What is a Logical Shift?

In a computer processor, registers frequently need to multiply or divide integers rapidly. Rather than executing complex arithmetic multiplication algorithms, the CPU can simply shift bits along the register.

- **Logical Left Shift:** Bits move to the left. Vacated positions on the right are filled with **0s**.
- **Logical Right Shift:** Bits move to the right. Vacated positions on the left are filled with **0s**.

---

### 2. Logical Left Shift (Multiplication by $2^n$)

Each single logical shift to the left **multiplies the integer value by 2**. Shifting $n$ places to the left multiplies the number by $2^n$:

  
| Shift Distance | Multiplier ($2^n$) | Example Starting Value | Shifted Value |
| --- | --- | --- | --- |
| Left 1 place | $\times 2^1 = 2$ | $00000101_2 \ (5_{10})$ | $00001010_2 \ (10_{10})$ |
| Left 2 places | $\times 2^2 = 4$ | $00000101_2 \ (5_{10})$ | $00010100_2 \ (20_{10})$ |
| Left 3 places | $\times 2^3 = 8$ | $00000101_2 \ (5_{10})$ | $00101000_2 \ (40_{10})$ |

#### Overflow During Left Shifts

If any $1$ is shifted out of the Most Significant Bit (MSB), an **overflow error** occurs because the resulting value exceeds the 8-bit register capacity ($255_{10}$). The most significant information is lost, leaving an incorrect value in the register.

---

### 3. Logical Right Shift (Integer Division by $2^n$)

Each single logical shift to the right performs **integer division by 2** (discarding any fractional remainder). Shifting $n$ places to the right divides the number by $2^n$:

  
| Shift Distance | Divisor ($2^n$) | Example Starting Value | Shifted Value |
| --- | --- | --- | --- |
| Right 1 place | $\div 2^1 = 2$ | $00101000_2 \ (40_{10})$ | $00010100_2 \ (20_{10})$ |
| Right 2 places | $\div 2^2 = 4$ | $00101000_2 \ (40_{10})$ | $00001010_2 \ (10_{10})$ |
| Right 3 places | $\div 2^3 = 8$ | $00101000_2 \ (40_{10})$ | $00000101_2 \ (5_{10})$ |

#### Precision Loss During Right Shifts

When an **odd number** is shifted right, the Least Significant Bit (LSB) contains a $1$. When shifted right, this bit falls off the end of the register and is discarded. For example, shifting $00001101_2$ ($13_{10}$) right by 1 gives $00000110_2$ ($6_{10}$). The fractional part ($.5$) is permanently lost, causing **precision loss**.

---

## Exam Tips & Common Pitfalls

> [!NOTE]
> **Direction Matters:** Remember: **L**eft = **L**arger (multiplication by $2^n$). **R**ight = **R**educe (division by $2^n$).
> **Always Pad to 8 Bits:** In Cambridge exam answers, write full 8-bit binary bytes showing all leading and trailing zeros.

---

## Key Terminology Glossary

- **Logical Shift:** An operation that moves all bits in a register left or right, filling empty positions with 0.
- **Vacated Bit:** A bit position that becomes empty following a shift operation and is replenished with 0.
- **Precision Loss:** The loss of fractional numerical detail when bits shifted out of the LSB are discarded.
- **Overflow:** When bits shifted out of the MSB cause the number to exceed the storage capacity of the register.

## Written practice and self-review

An 8-bit register holds the binary pattern 00001101 (denary 13). A programmer performs a logical binary shift of 1 place to the right. Explain what happens to the least significant bit, state the resulting binary pattern and denary value, and explain why this causes precision loss.

Write an explanation before checking the theory. Check that each claim answers the question, uses the correct concept and explains why it applies. Discuss uncertain points with a teacher. The structured exercise below checks specific facts; it does not automatically mark this explanation.

---

## Interactive Exercises & Autotest Specifications

### Exercise 1: Exam-Style Check: Logical Shift Effect (Multiple choice, 25 XP)

**Instructions:**
What is the mathematical effect of performing a logical binary shift of 3 places to the left on a positive integer, assuming no bits are lost?

**Options:**
- **A**: Multiplies the integer by 3
- **B**: Divides the integer by 3
- **C**: Multiplies the integer by 8 (2^3)
- **D**: Divides the integer by 8 (2^3)



### Exercise 2: Exam-Style Question: Register Shift Operations (Fill in blank, 35 XP)

**Instructions:**
An 8-bit register contains the binary number 00101100 (denary 44). Perform the following independent logical shifts:


**Starter Code:**
```text
{"prompt":"An 8-bit register contains 00101100 (denary 44).\nCalculate the result of:\n1. Shifting the original pattern 2 places to the LEFT.\n2. The denary value of that left-shifted result.\n3. Shifting the original pattern 1 place to the RIGHT.","template":"1. 8-bit pattern after left shift 2 places: [Blank 1]\n2. Denary value of result 1: [Blank 2]\n3. 8-bit pattern after right shift 1 place: [Blank 3]","word_bank":["10110000","176","00010110","22","01011000","88"],"blanks":[{"id":"blank_1"},{"id":"blank_2"},{"id":"blank_3"}]}
```


### Exercise 3: Application: Check the reasoning (Fill in blank, 30 XP)

**Instructions:**
Work out each answer before submitting. Shift unsigned 00010110 left by two places (eight bits): Shift unsigned 00010111 right by one place (eight bits): Does shifting 10000000 left once lose a 1: yes or no?


**Starter Code:**
```text
{"prompt":"Application: Check the reasoning","template":"1. Shift unsigned 00010110 left by two places (eight bits): [blank_1]\n2. Shift unsigned 00010111 right by one place (eight bits): [blank_2]\n3. Does shifting 10000000 left once lose a 1: yes or no? [blank_3]","blanks":[{"id":"blank_1"},{"id":"blank_2"},{"id":"blank_3"}],"word_bank":["01011000","00001011","yes","no","00101100","00000101","10110000"]}
```


