2.1.4 Binary addition and shifts
Binary Addition
Binary addition works exactly like denary addition, but with only two digits (0 and 1). The addition rules are:
| A | B | Sum | Carry |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
| 1+1+1 (with carry) | 1 | 1 |
Worked example: 01101011 + 00110110
01101011 (107) + 00110110 ( 54) ────────── 10100001 (161)
Working right to left: 1+0=1; 1+1=10 (write 0, carry 1); 0+1+1=10 (write 0, carry 1); 1+0+1=10 (write 0, carry 1); 0+1+1=10 (write 0, carry 1); 1+1+1=11 (write 1, carry 1); 1+0+1=10 (write 0, carry 1); 0+0+1=1. Result: 10100001 = 161.
Binary Shifts
A binary shift moves all bits in a number left or right by a given number of positions. Bits shifted off the end are lost (or may trigger an overflow). Gaps are filled with 0s.
A logical left shift by n positions multiplies the value by 2n (provided no significant bits are lost). Vacated positions on the right are filled with 0s.
Example: logical left shift of 00001100 by 2 positions
00001100 (12) → shift left 2 → 00110000 (48)
12 × 2² = 12 × 4 = 48 ✓. Two zeros fill from the right; the leftmost two bits (00) are shifted out.
If a 1-bit is shifted off the left end, the result is incorrect — this is an overflow.
A logical right shift by n positions divides the value by 2n (integer division — remainder is discarded). Vacated positions on the left are filled with 0s.
Example: logical right shift of 01011000 by 3 positions
01011000 (88) → shift right 3 → 00001011 (11)
88 ÷ 2³ = 88 ÷ 8 = 11 ✓. Three zeros fill from the left; the rightmost three bits (000) are discarded.
An arithmetic right shift preserves the sign bit (MSB) when shifting right, making it work correctly for signed (two's complement) numbers. The MSB is copied into vacated positions rather than filling with 0.
Example: arithmetic right shift of 11110000 (−16) by 2
11110000 → shift right 2, MSB copied → 11111100
−16 ÷ 4 = −4. Check: −128+64+32+16+8+4 = −128+124 = −4 ✓. If logical (fill with 0) were used: 00111100 = +60, which is wrong for a negative number.
An arithmetic left shift is the same as a logical left shift — it simply multiplies by 2.
Key Takeaways
- Binary addition: 1+1 = 10 (write 0, carry 1); 1+1+1 = 11 (write 1, carry 1).
- Logical left shift by n: multiplies by 2n; zeros fill from the right.
- Logical right shift by n: divides by 2n (integer); zeros fill from the left.
- Arithmetic right shift: sign bit is preserved (copied in) — correct for signed numbers.
- Bits shifted off the end are lost; a 1-bit lost from the left during a left shift causes overflow.