1.1 Data Representation – Cambridge A‑Level Computer Science (9618)
Learning Objectives
- Perform binary addition and subtraction (direct borrow and two’s‑complement).
- Convert between binary, decimal, hexadecimal and BCD.
- Understand binary prefixes (IEC) and how they differ from SI prefixes.
- Detect overflow and under‑flow for both unsigned and signed arithmetic.
- Explain why two’s‑complement is the standard signed representation in hardware.
- Identify the purpose of ASCII, extended ASCII and Unicode character encodings.
1. Number‑System Overview
| System | Base | Digits used | Typical use |
|---|
| Binary | 2 | 0, 1 | All internal computer data |
| Decimal (Denary) | 10 | 0‑9 | Human‑readable numbers |
| Hexadecimal | 16 | 0‑9, A‑F | Compact view of binary (4 bits = 1 hex digit) |
| BCD (Binary‑Coded Decimal) | 10 (packed as 4‑bit nibbles) | 0000‑1001 per nibble | Financial calculators, some legacy hardware where exact decimal representation is required |
| One’s complement | 2 (signed) | 0, 1 (with sign bit) | Historical signed representation |
| Two’s complement | 2 (signed) | 0, 1 (with sign bit) | Standard signed representation in modern CPUs |
1.1 Binary ↔ Decimal ↔ Hexadecimal Conversions
- Binary → Decimal: multiply each bit by 2ⁿ (n = position from right, starting at 0) and sum.
- Decimal → Binary: repeatedly divide by 2, recording remainders (or use powers of 2).
- Binary → Hexadecimal: group binary digits in sets of four (starting from the right) and replace each group with its hex equivalent.
- Hexadecimal → Binary: replace each hex digit with its 4‑bit binary pattern.
| Binary | Decimal | Hex |
|---|
| 0000 | 0 | 0 |
| 0001 | 1 | 1 |
| 0010 | 2 | 2 |
| 0011 | 3 | 3 |
| 0100 | 4 | 4 |
| 0101 | 5 | 5 |
| 0110 | 6 | 6 |
| 0111 | 7 | 7 |
| 1000 | 8 | 8 |
| 1001 | 9 | 9 |
| 1010 | 10 | A |
| 1011 | 11 | B |
| 1100 | 12 | C |
| 1101 | 13 | D |
| 1110 | 14 | E |
| 1111 | 15 | F |
1.2 BCD Conversion Example
Decimal 27 → BCD (packed):
- Separate each decimal digit: 2 and 7.
- Convert each digit to a 4‑bit binary nibble: 2 →
0010, 7 → 0111. - Combine the nibbles:
0010 0111.
1.3 Binary Prefixes (IEC) vs. SI Prefixes
| IEC Prefix | Symbol | Value (powers of 2) | SI Equivalent (powers of 10) |
|---|
| kibi | Ki | 2¹⁰ = 1 024 | k (kilo) = 10³ |
| mebi | Mi | 2²⁰ = 1 048 576 | M (mega) = 10⁶ |
| gibi | Gi | 2³⁰ = 1 073 741 824 | G (giga) = 10⁹ |
| tebi | Ti | 2⁴⁰ = 1 099 511 627 776 | T (tera) = 10¹² |
Example: 1 MiB = 2²⁰ bytes = 1 048 576 bytes, whereas 1 MB (SI) = 10⁶ bytes = 1 000 000 bytes. The IEC prefixes remove the ambiguity that can arise when manufacturers use SI prefixes for memory.
2. Binary Addition
2.1 Addition Rules (Truth Table)
| A | B | Sum | Carry |
|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
2.2 Step‑by‑Step Example (Unsigned)
Add 1011₂ + 0110₂ (4‑bit words):
1 0 1 1
+ 0 1 1 0
----------
Carry: 1 1 0 0
Result:1 0 0 0 1
Result = 10001₂ (decimal 17). The extra carry indicates unsigned overflow for a 4‑bit register.
2.3 Overflow Detection
- Unsigned addition: overflow occurs when a carry is generated out of the most‑significant bit (MSB).
- Signed (two’s‑complement) addition:
- Let Cin be the carry into the sign bit and Cout the carry out of the sign bit.
- Overflow = Cin ⊕ Cout (XOR). If they differ, the signed result cannot be represented.
2.4 Signed‑Overflow Example
8‑bit signed addition: 0110 1101₂ (109) + 0101 0011₂ (83)
0 1 1 0 1 1 0 1
+ 0 1 0 1 0 0 1 1
-----------------
1 0 0 0 0 0 0 0
- Carry into the sign bit = 0, carry out = 1 → XOR = 1 → signed overflow.
- Correct 8‑bit two’s‑complement result is
1000 0000₂, which represents –128, not the expected +192.
3. Binary Subtraction
3.1 Direct Borrow Method
Borrow works exactly as in decimal, but a borrow means “take 2” from the next higher bit.
Example: 10101₂ – 00111₂
1 0 1 0 1
- 0 0 1 1 1
------------
0 1 1 1 0 (binary 14)
3.2 One’s‑Complement Subtraction (Unsigned)
- Form the one’s complement of the subtrahend (invert every bit).
- Add this to the minuend.
- If a carry out of the MSB occurs, perform an end‑around carry by adding that carry back to the LSB.
Works for unsigned numbers but requires the extra end‑around step.
3.3 Two’s‑Complement Subtraction (Preferred)
- Choose a word length that can hold both operands (including a sign bit).
- Write the subtrahend in binary.
- Invert all bits (one’s complement).
- Add 1 → two’s complement (the negative of the subtrahend).
- Add this negative to the minuend.
- If a carry out of the MSB occurs, discard it for signed results (it is ignored for two’s‑complement arithmetic).
Example (5‑bit word): 10101₂ – 00111₂
- Subtrahend =
00111 - One’s complement =
11000 - Two’s complement =
11001 (add 1) - Add to minuend:
1 0 1 0 1
+ 1 1 0 0 1
-------------
1 0 0 1 0 0
- Discard the left‑most carry →
01110₂ (decimal 14).
3.4 Signed‑Overflow in Subtraction
Overflow in subtraction can be detected using the same XOR rule as for addition, because subtraction is performed by adding the two’s‑complement of the subtrahend.
Example: 8‑bit 0100 0000₂ (64) – 1100 0000₂ (‑64)
- Two’s‑complement of
1100 0000 = 0100 0000. - Adding yields
1000 0000 with carry‑in = 0, carry‑out = 1 → XOR = 1 → overflow (result would be 128, which cannot be represented in 8‑bit two’s‑complement).
4. Character Encoding
Exercise: Write the 8‑bit ASCII binary code for the character ‘g’ (ASCII 103).
Solution: 103₁₀ = 1100111₂ → padded to 8 bits → 01100111.
5. Practical Applications & Rationale
- Two’s complement is used in virtually all modern CPUs because a single adder circuit can perform both addition and subtraction. The negative of a number is obtained by inverting the bits and adding 1, so subtraction reduces to addition.
- BCD is employed where exact decimal representation is mandatory (e.g., financial calculators, some embedded systems) because each decimal digit is stored separately, avoiding binary‑fraction rounding errors.
- Hexadecimal is the preferred notation for programmers when viewing memory addresses or machine code, as each hex digit maps directly to a 4‑bit nibble.
- Binary prefixes (IEC) are used for memory and storage specifications to avoid the ambiguity of SI prefixes (k, M, G) which are based on powers of 10.
- Floating‑point teaser (A‑Level extension): After mastering integer representations, the next step is the IEEE 754 floating‑point format, which stores numbers in a sign, exponent and mantissa.
6. Practice Questions (Cambridge‑style)
- Binary addition – Add
1101₂ and 0111₂. Show each column, the carry, and state whether overflow occurs for unsigned 4‑bit addition. - Direct borrow subtraction – Subtract
0101₂ from 1010₂. Show the borrowing steps. - Two’s‑complement subtraction – Using a 4‑bit word, subtract
0010₂ from 0110₂. Include the formation of the two’s complement, the addition, and the final result. - Conversion – Convert the binary number
101101₂ to decimal, hexadecimal and packed BCD. - Signed overflow detection – For the signed 8‑bit addition
0110 1101₂ + 0101 0011₂, determine the decimal values, perform the addition, and state whether signed overflow occurs. Explain using the carry‑into‑sign‑bit XOR rule. - Character encoding – Write the 8‑bit ASCII binary code for the string “Hi!”. Then give the corresponding hexadecimal representation.
- Explain (AO2/AO3) – In 150 words, explain why two’s‑complement allows a computer to use a single hardware adder for both addition and subtraction. Include a brief note on overflow handling.
- Pseudocode (AO2) – Write a short pseudocode routine
Subtract_TwosComp(A, B, n) that returns A – B using two’s‑complement arithmetic on n-bit words.
Sample Solution Sketch for Question 5 (Signed Overflow)
Binary values:
0110 1101₂ = +109₁₀0101 0011₂ = +83₁₀
Binary addition:
0 1 1 0 1 1 0 1
+ 0 1 0 1 0 0 1 1
-----------------
1 0 0 0 0 0 0 0
Carry into sign bit = 0, carry out = 1 → XOR = 1 → signed overflow. The 8‑bit result 1000 0000₂ represents –128, not the mathematically correct +192.
7. Summary
- Binary addition follows the simple sum‑and‑carry rules; unsigned overflow is indicated by a final carry, signed overflow by
CarryintoMSB ⊕ Carryoutof_MSB. - Binary subtraction can be performed by direct borrowing, but two’s‑complement subtraction (adding the negative) is faster and aligns with hardware design.
- Two’s‑complement is the universal signed representation because it removes the end‑around‑carry step and enables a single adder circuit to handle both addition and subtraction.
- Understanding hexadecimal, BCD, and binary prefixes is essential for interpreting machine code, memory sizes, and decimal‑oriented applications.
- ASCII, extended ASCII and Unicode provide progressively larger character sets; Unicode’s variable‑length encoding solves the limitation of 8‑bit code pages.