Use the two’s complement number system to represent positive and negative 8-bit binary integers

Data Representation – Two’s Complement (8‑bit)

Learning objectives

  • Convert between decimal, binary and hexadecimal for 8‑bit signed integers.
  • Represent positive and negative numbers using two’s‑complement.
  • Perform binary addition, subtraction and detect overflow.
  • Apply logical left‑ and right‑shifts and understand their arithmetic effect.
  • Use overflow detection as a simple form of error‑checking (AO3).

1. Number‑system basics (Topic 1.1)

1.1 Decimal ↔ Binary (positive numbers)

Repeated division by 2 gives the binary digits (remainders). Pad the result with leading 0’s to obtain an 8‑bit pattern.

45 ÷ 2 = 22 r 1
22 ÷ 2 = 11 r 0
11 ÷ 2 = 5 r 1
 5 ÷ 2 = 2 r 1
 2 ÷ 2 = 1 r 0
 1 ÷ 2 = 0 r 1   → binary 101101 → 8‑bit 00101101

1.2 Decimal ↔ Hexadecimal (positive numbers)

Convert the decimal number to binary, group the bits into nibbles (4‑bit groups) and replace each nibble by its hex digit.

45₁₀ → 0010 1101₂ → 2D₁₆

Conversely, to go from hexadecimal to decimal, expand each hex digit to its 4‑bit binary form and then evaluate the binary value.

1.3 Binary ↔ Hexadecimal (quick check)

  • Binary 1010 1100 → nibble 1010=A, 1100=C → AC₁₆.
  • Hex 7F → binary 0111 1111.

1.4 Simple 8‑bit addition and overflow detection (Topic 1.2)

When two 8‑bit numbers are added, any carry out of the most‑significant bit (MSB) is discarded. Overflow occurs when the carry into the sign bit differs from the carry out of the sign bit.

  00101101   (+45)
+ 00010110   (+22)
------------ 
  00111111   (+67)   ← no overflow (carry‑in = carry‑out = 0)

  01111111   (+127)
+ 00000001   (+1)
------------ 
  10000000   (‑128) ← overflow: carry‑in = 0, carry‑out = 1

2. Two’s‑complement representation (Topic 1.2‑1.3)

2.1 Range and sign bit

In an 8‑bit two’s‑complement word the most‑significant bit (MSB) is the sign bit. The representable range is

–2⁷ … 2⁷‑1 = –128 to 127

2.2 Positive numbers

  • Write the decimal value in binary.
  • Pad with leading 0’s to 8 bits (the sign bit is 0).

2.3 Negative numbers – conversion algorithm

  1. Write the absolute value N in 8‑bit binary.
  2. Invert every bit (one’s complement).
  3. Add 1 to the result.

The final 8‑bit pattern is the two’s‑complement representation of –N. The sign bit is 1.

2.4 Why addition and subtraction work automatically

  • Addition: Adding a number to its two’s‑complement negative yields 00000000 (the final carry is discarded).
  • Subtraction: To compute A – B, add A to the two’s‑complement of B. The hardware performs only addition.
45 – 23
  00101101   (+45)
+ 11101001   (‑23)   ← two’s‑complement of 23
------------ 
  00010110   (+22)   ← correct result, no overflow

2.5 Overflow detection (AO3)

After an 8‑bit addition, examine the two carries around the sign bit:

  • If they are different → overflow (the true result is outside –128 … 127).
  • If they are the same → the result is valid.

In programmes this test is used to flag arithmetic errors before using the result.

2.6 Logical shifts (Topic 1.3)

  • Logical left shift (<< n): move all bits n places left, inserting 0’s on the right. Effectively multiplies the unsigned value by 2ⁿ (mod 256). For signed two’s‑complement numbers the sign bit may change, so overflow can occur.
  • Logical right shift (>> n): move all bits n places right, inserting 0’s on the left. Equivalent to floor division by 2ⁿ for unsigned values; for signed numbers it performs an arithmetic right shift only if the sign bit is replicated (not required for this syllabus).

Examples (8‑bit):

00110101₂ (53) << 1 → 01101010₂ (106)
10011010₂ (‑102) << 1 → 00110100₂ (52)  ← sign bit flipped → overflow
01101010₂ (106) >> 1 → 00110101₂ (53)

3. Worked examples

3.1 Positive 45

Binary: 00101101 Hex: 2D

3.2 Negative –45

45 → 00101101
invert → 11010010
+1 → 11010011
Result: 11010011₂ = D3₁₆

3.3 Subtraction example: 45 – 23

45 → 00101101
23 → 00010111 → invert → 11101000 → +1 → 11101001 (‑23)
Add:
 00101101
+11101001
---------
 00010110  (= 22₁₀)

3.4 Overflow illustration (adding 1 to the maximum)

01111111 (+127)
+00000001 (+1)
------------
10000000 (‑128)   ← overflow (carry‑in = 0, carry‑out = 1)

3.5 Logical shift examples

Left shift: 00110101 (53) << 1 → 01101010 (106)
Right shift: 01101010 (106) >> 1 → 00110101 (53)

4. Conversion table (selected values)

Decimal 8‑bit Two’s‑Complement Hexadecimal
-1281000000080
-100100111009C
-6411000000C0
-4511010011D3
-2311101001E9
-111111111FF
00000000000
10000000101
15000011110F
230001011117
45001011012D
640100000040
850101010155
1000110010064
127011111117F

5. Practice exercises

  1. Write the 8‑bit two’s‑complement representation for:
    • +23
    • –23
    • +85
    • –85
    • +0
    • –0 (trick question)
  2. Given the 8‑bit pattern 11101010, state the decimal value it represents in two’s complement.
  3. Show, using binary addition, that adding 45 and –45 yields 0 (discard the final carry).
  4. Demonstrate what happens when you add 1 to +127 in 8‑bit two’s complement, and indicate whether overflow has occurred.
  5. Perform a logical left shift on 00110101 and give the decimal result.
  6. Calculate 45 – 23 using two’s‑complement subtraction and state whether overflow occurs.

6. Common mistakes to avoid

  • Forgetting to pad positive numbers with leading 0’s to reach 8 bits.
  • Inverting bits **after** adding 1 – the correct order is invert first, then add 1.
  • Assuming 10000000 represents –0; in two’s complement there is only one zero: 00000000.
  • Using sign‑magnitude or excess‑bias methods when the question explicitly asks for two’s complement.
  • Ignoring overflow: adding two numbers that exceed the –128 … 127 range wraps around and changes the sign.
  • Treating a logical left shift as a safe multiplication – it can cause overflow and change the sign bit.

7. Summary

Two’s complement provides a single binary format for signed integers. In an 8‑bit system the sign bit is the MSB and the range is –128 to 127. Positive values are written directly; negative values are obtained by inverting the magnitude and adding 1. The same adder circuitry can perform addition, subtraction (by adding the two’s‑complement of the subtrahend) and logical shifts. Overflow is detected by comparing the carry into and out of the sign bit, a technique useful for simple error‑checking in programmes. Mastery of these conversions underpins all low‑level programming and debugging tasks required by the Cambridge IGCSE 0478 syllabus.

8. Further reading (Cambridge syllabus cross‑references)

  • Topic 1.1 – Number systems (binary, decimal, hexadecimal).
  • Topic 1.2 – Binary arithmetic, overflow detection, two’s‑complement subtraction.
  • Topic 1.3 – Logical operations, shifts, and their effect on data.
  • Topic 2.1 – Representing text, images and sound (uses binary/hexadecimal formats).
  • Topic 2.4 – Data compression and error detection (relies on binary representations and overflow checks).
Suggested diagram: Flowchart showing the steps to convert a negative decimal number to its 8‑bit two’s‑complement binary representation.

Create an account or Login to take a Quiz

55 views
0 improvement suggestions

Log in to suggest improvements to this note.