Apply a binary shift
What is a Binary Shift?
A binary shift moves every bit in an 8-bit pattern left or right by a specified number of positions. AQA requires only the logical shift: every position vacated by the moving bits is filled with a 0, and any bit that moves beyond the 8-bit boundary is permanently lost.
Shifts have a precise mathematical effect. A left shift by n positions multiplies the value by 2n, and a right shift by n positions performs integer division by 2n. Any remainder from a right shift is simply discarded - no fractions are stored.
| Direction | Positions | Mathematical effect |
|---|---|---|
| Left | 1 | × 2 |
| Left | 2 | × 4 |
| Left | 3 | × 8 |
| Right | 1 | ÷ 2 (integer) |
| Right | 2 | ÷ 4 (integer) |
| Right | 3 | ÷ 8 (integer) |
Worked Examples
Left Shift by 2
Shift 00001011 (11) left by 2. Every bit moves 2 positions to the left; two 0s fill the vacated positions on the right.
| b7 | b6 | b5 | b4 | b3 | b2 | b1 | b0 | Decimal | |
|---|---|---|---|---|---|---|---|---|---|
| Before | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 1 | 11 |
| After | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 0 | 44 |
Yellow cells show the two zeros filled on the right. Result: 00101100 = 44. Check: 11 × 4 = 44 ✓
Right Shift by 2
Shift 10110111 (183) right by 2. Every bit moves 2 positions to the right; two 0s fill the left. The two rightmost bits are permanently discarded.
| b7 | b6 | b5 | b4 | b3 | b2 | b1 | b0 | Decimal | |
|---|---|---|---|---|---|---|---|---|---|
| Before | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 183 |
| After | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 45 |
Red cells show the two discarded bits (both 1s - representing a remainder of 0.75). Yellow cells show the zeros filled on the left. Result: 00101101 = 45. Exact: 183 ÷ 4 = 45.75, but the remainder is dropped. Check: 183 ÷ 4 = 45 (integer) ✓
Left Shift with Overflow
Shift 01100000 (96) left by 2. The bit at b6 (value 64) would move to position b8 - beyond the 8-bit boundary - and is lost. This is called overflow.
| b7 | b6 | b5 | b4 | b3 | b2 | b1 | b0 | Decimal | |
|---|---|---|---|---|---|---|---|---|---|
| Before | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 96 |
| After | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 128 |
The red bit (b6 = 1) would shift to b8 - lost. Only the bit at b5 (value 32) successfully shifts to b7, giving 10000000 = 128. Expected result: 96 × 4 = 384, but 384 exceeds 255 and cannot fit in 8 bits. The multiplication relationship breaks down whenever overflow occurs.
Key Takeaways
- A logical left shift by n positions moves all bits left, fills n zeros on the right, and multiplies the value by 2n.
- A logical right shift by n positions moves all bits right, fills n zeros on the left, and integer-divides the value by 2n - any remainder is discarded.
- Overflow occurs during a left shift when a 1-bit is pushed beyond b7; the multiplication result is incorrect because data has been permanently lost.
- For AQA GCSE: numbers are always 8 bits, only logical shifts are required, and results are never expressed as fractions.