Year 10 · Computer Science · Autumn 1

Data representation · Lesson 3 of 5

Two's complement

Theory1.1 Number systems

Year 10 · Computer Science · Data representation · Lesson 3 of 5

You will learn to

  • Explain that in 8-bit two's complement the left-most bit is worth −128.
  • Convert negative denary numbers to 8-bit two's complement, and back.
  • State the range of 8-bit two's complement: −128 to +127.
  • Add a positive and a negative number in two's complement and check the answer.

Year 10 · Computer Science · Data representation · Lesson 3 of 5

Success criteria

You have got it when you can say:

  • I can convert −37 to 11011011 and show my working.
  • I can convert 11111111 back to denary and get −1.
  • I can explain why 8-bit two's complement cannot hold +128.

Year 10 · Computer Science · Data representation · Lesson 3 of 5

Retrieval starter

Questions from before. Write your answers, then show them.

  1. Last lesson

    Hex B2 in binary is…

    A 1011 0010B 1100 0010C 1011 0100

    A. 1011 0010

  2. Earlier this unit

    Adding 11001100 and 01011010 in an 8-bit register gives…

    A 00100110 with no problemB An overflow errorC 11111111

    B. An overflow error

Year 10 · Computer Science · Data representation · Lesson 3 of 5

Key words (1 of 3)

Bit
A single binary digit, 0 or 1.
Nibble
A group of 4 bits, which matches one hexadecimal digit.
Byte
A group of 8 bits.
Denary
The base 10 number system we use every day, with digits 0 to 9.

Year 10 · Computer Science · Data representation · Lesson 3 of 5

Key words (2 of 3)

Binary
The base 2 number system computers use, with digits 0 and 1.
Hexadecimal
The base 16 number system, with digits 0 to 9 and A to F, used to write binary in a short form.
Overflow
An error when a result is too big to fit in the number of bits available.
Logical shift
Moving all the bits left or right, losing bits off one end and filling the other end with 0s.

Year 10 · Computer Science · Data representation · Lesson 3 of 5

Key words (3 of 3)

Two's complement
A way to store negative numbers, where the leftmost bit has a negative place value (−128 in 8 bits).
Colour depth
The number of bits used to store the colour of each pixel.

The explanation · step 1 of 15 · part 1 of 2

Why computers use binary

A computer is made of billions of tiny switches called transistors. Each switch is either off or on.

We write off as 0 and on as 1. A number system with only two digits is called binary.

One binary digit is a bit. Eight bits together make a byte.

Every extra bit doubles the number of different patterns you can make.

The explanation · step 1 of 15 · part 2 of 2

Why computers use binary (continued)

e.g.Example
1 bit: 0, 1 → 2 patterns 2 bits: 00, 01, 10, 11 → 4 patterns 3 bits: 000, 001, 010, 011, 100, 101, 110, 111 → 8 patterns 8 bits: 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 = 256 patterns
?Why?

Two states are very reliable. A switch is clearly on or clearly off, so the computer rarely misreads a value.

The explanation · step 1 of 15

The maths: How many patterns can n bits make?

The explanation · step 2 of 15 · part 1 of 3

Place value: denary and binary

Denary is our everyday number system. It has ten digits, 0 to 9, and each column is worth 10 times the column to its right.

Binary works the same way, but each column is worth 2 times the column to its right.

For 8 bits the column values, from left to right, are 128, 64, 32, 16, 8, 4, 2, 1.

To turn binary into denary, add up the column values that have a 1 under them.

The explanation · step 2 of 15 · part 2 of 3

Place value: denary and binary (continued)

Try the toy: switch on the 128 bit. What happens to the denary value? Now make 255.

1286432168421
01011010
e.g.Example
Binary 01011010 to denary. The 1s sit under 64, 16, 8 and 2. 64 + 16 + 8 + 2 = 90

The explanation · step 2 of 15 · part 3 of 3

Place value: denary and binary (continued)

!Watch out

Always write the column values above the bits first. Most mistakes come from adding the wrong column.

There is something to try for this step in the IGCSE library.

The explanation · step 3 of 15 · part 1 of 2

Denary to binary

Start at the left column, 128. Ask: does 128 fit into my number?

If it fits, write 1 and take 128 away. If it does not fit, write 0.

Move to the next column and repeat with what is left. Stop when you reach the 1 column.

Always write all 8 bits, including the 0s at the front.

The explanation · step 3 of 15 · part 2 of 2

Denary to binary (continued)

e.g.Example
Denary 150 to binary. 128 fits → 1. Left: 150 − 128 = 22 64 does not fit → 0 32 does not fit → 0 16 fits → 1. Left: 22 − 16 = 6 8 does not fit → 0 4 fits → 1. Left: 6 − 4 = 2 2 fits → 1. Left: 0 1 does not fit → 0 Answer: 10010110 Check: 128 + 16 + 4 + 2 = 150
?Why?

Each column is used once at most, so taking away the biggest column first always gives the right pattern.

The explanation · step 4 of 15 · part 1 of 2

The biggest number n bits can hold

With n bits you get 2ⁿ patterns. One of those patterns is 0.

So the biggest number is one less than the number of patterns: 2ⁿ − 1.

8 bits hold 0 to 255. 16 bits hold 0 to 65,535.

A register is a small store inside the processor. An 8-bit register cannot hold anything bigger than 255.

The explanation · step 4 of 15 · part 2 of 2

The biggest number n bits can hold (continued)

e.g.Example
8 bits: 2⁸ = 256 patterns. Biggest = 256 − 1 = 255 (11111111) 16 bits: 2¹⁶ = 65,536 patterns. Biggest = 65,536 − 1 = 65,535 (1111111111111111, or FFFF in hex) Quick check for 8 bits: 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255
!Watch out

8 bits give 256 different values, but the largest value is 255, because counting starts at 0.

The explanation · step 6 of 15 · part 1 of 2

Hex to denary and denary to hex

Hex to denary: multiply each digit by its column value, then add. Change letters to numbers first (B = 11, F = 15).

Denary to hex: divide by 16. The whole-number answer is the left digit. The remainder is the right digit.

Change any digit from 10 to 15 into its letter.

The explanation · step 6 of 15 · part 2 of 2

Hex to denary and denary to hex (continued)

e.g.Example
Hex B7 to denary: B = 11, so 11 × 16 = 176 7 × 1 = 7 176 + 7 = 183 Denary 200 to hex: 200 ÷ 16 = 12 remainder 8 12 = C, so the answer is C8 Check: 12 × 16 + 8 = 192 + 8 = 200 Three digits, hex 1A3 to denary: 1 × 256 = 256 A = 10, so 10 × 16 = 160 3 × 1 = 3 256 + 160 + 3 = 419
!Watch out

A remainder of 10 to 15 must be written as a letter. Writing "128" for C8 is a common slip.

The explanation · step 6 of 15

The maths: Hex to denary

The explanation · 10 more steps

More on the lesson page

The lesson page has the rest of the explanation, step by step:

  • Hexadecimal: base 16
  • Binary and hex using nibbles
  • Why hexadecimal is used
  • Adding binary: the four rules
  • A full 8-bit addition
  • Overflow
  • Logical left shift
  • Logical right shift
  • Two's complement: negative numbers
  • Denary to two's complement

Open the lesson page

Year 10 · Computer Science · Data representation · Lesson 3 of 5

Quick quiz · 1 of 3

Shifting 00001010 (10) one place left gives…

  1. A 00000101 (5)
  2. B 00010100 (20)
  3. C 00001011 (11)

B. 00010100 (20)

A left shift doubles. 10 becomes 20.

Year 10 · Computer Science · Data representation · Lesson 3 of 5

Quick quiz · 2 of 3

In 8-bit two's complement, 11111111 is…

  1. A −1
  2. B −127
  3. C 255

A. −1

The left bit is worth −128. −128 + 127 = −1.

Year 10 · Computer Science · Data representation · Lesson 3 of 5

Quick quiz · 3 of 3

Which of these is lossy?

  1. A ZIP
  2. B JPEG
  3. C PNG

B. JPEG

JPEG throws away detail. PNG and ZIP give every bit back.

Year 10 · Computer Science · Data representation · Lesson 3 of 5

Exit ticket

Answer on paper before you go. Then check.

  1. A 10 × 10 image at 8 bits per pixel takes…

    A 8,000 bitsB 800 bits = 100 bytesC 80 bytes

    B. 800 bits = 100 bytes 10 × 10 × 8 = 800 bits. Divide by 8 for bytes.

  2. How many bits are in a byte?

    A 8B 16C 4

    A. 8 A byte is 8 bits. 4 bits is a nibble.