Number Systems
Computers work exclusively with binary (base-2), but we think in denary (base-10), and programmers often use hexadecimal (base-16) as a compact notation. Mastering conversions between these systems — and performing binary arithmetic — is essential for every CS topic from memory addressing to data representation.
Learning Objectives
- 12.3.5.1 Convert between binary, denary, and hexadecimal
- 12.3.5.2 Perform binary addition, subtraction, and shifts
The Three Number Systems
| System | Base | Digits | Example | Use |
|---|---|---|---|---|
| Denary | 10 | 0–9 | 255 | Human counting |
| Binary | 2 | 0, 1 | 11111111 | Computer storage & processing |
| Hexadecimal | 16 | 0–9, A–F | FF | Compact binary notation, colour codes, memory addresses |
Hex Digit Values
A=10, B=11, C=12, D=13, E=14, F=15. Each hex digit = exactly 4 binary bits (a nibble).
Denary ↔ Binary Conversion
✓ Denary → Binary (Repeated Division)
Convert 156 to binary:
156 ÷ 2 = 78 remainder 0 ↑
78 ÷ 2 = 39 remainder 0 │
39 ÷ 2 = 19 remainder 1 │ Read remainders
19 ÷ 2 = 9 remainder 1 │ from BOTTOM
9 ÷ 2 = 4 remainder 1 │ to TOP
4 ÷ 2 = 2 remainder 0 │
2 ÷ 2 = 1 remainder 0 │
1 ÷ 2 = 0 remainder 1 ┘
Result: 156₁₀ = 10011100₂✓ Binary → Denary (Place Values)
Convert 10011100 to denary:
Position: 128 64 32 16 8 4 2 1
Binary: 1 0 0 1 1 1 0 0
= 128 + 16 + 8 + 4 = 156
Result: 10011100₂ = 156₁₀Denary ↔ Hexadecimal Conversion
✓ Denary → Hex (Repeated Division by 16)
Convert 492 to hex:
492 ÷ 16 = 30 remainder 12 (C) ↑
30 ÷ 16 = 1 remainder 14 (E) │ Read bottom to top
1 ÷ 16 = 0 remainder 1 (1) ┘
Result: 492₁₀ = 1EC₁₆✓ Hex → Denary (Place Values)
Convert 1EC to denary:
Position: 256 16 1
(16²) (16¹) (16⁰)
Hex: 1 E C
Value: 1 14 12
= (1 × 256) + (14 × 16) + (12 × 1)
= 256 + 224 + 12
= 492
Result: 1EC₁₆ = 492₁₀Binary ↔ Hex (Quick Method)
✓ Binary → Hex: Group into nibbles (4 bits)
Convert 10011100 to hex:
1001 1100 ← Group into 4-bit nibbles (right to left)
9 C ← Convert each nibble to hex
Result: 10011100₂ = 9C₁₆✓ Hex → Binary: Expand each digit to 4 bits
Convert A3 to binary:
A → 1010
3 → 0011
Result: A3₁₆ = 10100011₂Binary Arithmetic
Binary Addition Rules
| A | B | Sum | Carry |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
✓ Binary Addition: 10110011 + 01001110
Carry: 1 1 1 1 0 0 1 0
1 0 1 1 0 0 1 1 (= 179)
+ 0 1 0 0 1 1 1 0 (= 78)
─────────────────
1 0 0 0 0 0 0 0 1 (= 257)
Check: 179 + 78 = 257 ✓
Note: Result is 9 bits → overflow if using 8-bit representation!Binary Shifts
| Shift | Action | Effect |
|---|---|---|
| Logical left shift | Shift all bits left, fill right with 0 | Multiply by 2 (per shift) |
| Logical right shift | Shift all bits right, fill left with 0 | Divide by 2 (per shift, integer division) |
✓ Shift Example
Original: 00010100 (= 20)
Left shift 1: 00101000 (= 40) → 20 × 2
Left shift 2: 01010000 (= 80) → 20 × 4
Right shift 1: 00001010 (= 10) → 20 ÷ 2
Right shift 2: 00000101 (= 5) → 20 ÷ 4Pitfalls & Common Errors
Forgetting to Pad Binary to 8 Bits
Always pad with leading zeros to fill the whole byte: 101 should be written as 00000101. This matters especially for hex conversions and shifts.
Reading Division Remainders Wrong Way
When converting denary to binary or hex, remainders are read bottom to top (last remainder = most significant digit). Reading top-to-bottom gives the wrong answer.
Overflow in Binary Addition
If the result needs more bits than available (e.g., 9 bits in an 8-bit system), this is overflow. The extra bit is lost and the answer wraps around. Always check for this.
Pro-Tips for Exams
Conversion Strategy
- For binary ↔ hex, always use the 4-bit grouping shortcut — it's the fastest
- For denary → binary, write the place values (128, 64, 32, 16, 8, 4, 2, 1) as headers first
- Always verify by converting back: if you convert 156 to binary, convert it back to check you get 156
- For addition: write carry bits above the calculation to stay organized
- For shifts: "left = ×2, right = ÷2" — easy mnemonic
Graded Tasks
Convert: (a) 217₁₀ to binary (b) 11010110₂ to denary (c) 3F7₁₆ to denary (d) 194₁₀ to hex
Convert: (a) 10101011₂ to hex (b) B4₁₆ to binary (c) 1A3₁₆ to binary
Add in binary: (a) 01100101 + 00111011 (b) 11001100 + 01010101
Perform the following shifts on 00110100: (a) left shift by 2 (b) right shift by 3. State the denary value before and after each shift.
Why do programmers prefer hexadecimal over binary for writing memory addresses? Use a specific example to illustrate your answer.