Book an info sessionInfo session

Signed integers: two’s complement and BCD

Cambridge A-Level Computer Science (9618) · lesson 2 of 15 · 30 min

Two’s Complement — A-Level Depth

You already know two’s complement from IGCSE. At A-Level, you need to handle overflow detection, sign extension, and arithmetic with multi-byte values.

Sign extension: When extending a two’s complement number to more bits, copy the MSB (sign bit) into all new high-order bits. E.g. 8-bit −5 (11111011) extended to 16-bit: 1111111111111011.

Overflow Detection in Two’s Complement

Overflow occurs in signed arithmetic when:

  • Adding two positive numbers gives a negative result.
  • Adding two negative numbers gives a positive result.

Detection rule: overflow has occurred if the carry into the MSB ≠ carry out of the MSB.

BCD — Binary Coded Decimal

BCD represents each decimal digit separately as a 4-bit binary group. It avoids the rounding errors of binary floating-point for financial calculations.

Advantage: No conversion error for decimal fractions. Disadvantage: Wastes space (4 bits per digit, but only 10 of 16 patterns used).

What is BCD for decimal 47?

  • 0100 0111
  • 0100 1000
  • 0011 0111
  • 0101 0111

Answer: 0100 0111
4 = 0100, 7 = 0111 → BCD is 0100 0111.

Sign extension of 8-bit 11001010 to 16 bits gives:

  • 0000000011001010
  • 1111111111001010
  • 1111111100110101
  • 0000000000110101
  • Answer: 1

The sign bit is 1, so extend with 1s: 1111111111001010. BCD is preferred over binary for financial calculations because:

  • It is faster
  • It avoids decimal rounding errors
  • It uses less memory
  • It supports negative numbers
  • Answer: 1

BCD stores each decimal digit exactly, avoiding the binary approximation of decimal fractions.


Previous lesson: Number systems and base conversion
Back to Cambridge A-Level Computer Science (9618)
Next lesson: Floating-point representation and normalisation