Skip to the page
Year 10 Β· Computer Science Β· Autumn 1 Β· Data representation

Two's complement

What this lesson covers

1.1 Number systems

Cambridge IGCSE Computer Science 0478 Β· Topic 1: Data representation Β· Paper 1

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.

Success criteria

  • 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.
The syllabus points for this section
  • Convert positive whole numbers between binary, denary and hexadecimal, both ways.
  • Explain why hexadecimal is used and give real uses.
  • Add two 8-bit binary numbers and spot overflow.
  • Carry out logical left and right shifts and explain their effect.
  • Use 8-bit two's complement for positive and negative numbers.
  • State the largest number that 8 bits and 16 bits can hold.

Key words

Say each word, then read what it means. Every one is in the glossary too.

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.
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.
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

Read it here, or open the study page below to go one step at a time with things to try.

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.

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.

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.

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
!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.

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.

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 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.

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.

Hexadecimal: base 16

Hexadecimal (hex) has sixteen digits. It uses 0 to 9, then the letters A to F for 10 to 15.

Each hex column is worth 16 times the column to its right. Two-digit hex has columns 16 and 1. Three digits add a 256 column.

One hex digit stands for exactly four bits. Four bits are called a nibble.

DenaryBinary (nibble)Hex
000000
100011
200102
300113
401004
501015
601106
701117
810008
910019
101010A
111011B
121100C
131101D
141110E
151111F
?Why?

16 is 2 Γ— 2 Γ— 2 Γ— 2. That is why one hex digit matches four bits exactly, with nothing left over.

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.

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.

Binary and hex using nibbles

Binary to hex: split the binary into groups of 4 bits, starting from the right. Change each group into one hex digit.

Hex to binary: change each hex digit into its 4-bit nibble. Write them side by side.

Each nibble uses the small column values 8, 4, 2, 1. This keeps the sums easy.

The fastest route between denary and hex is often through binary.

e.g.Example
Binary 10110110 to hex: Split: 1011 | 0110 1011 = 8 + 2 + 1 = 11 = B 0110 = 4 + 2 = 6 Answer: B6 Hex 4E to binary: 4 = 0100 E = 14 = 8 + 4 + 2 = 1110 Answer: 01001110 12 bits, 000110100011 to hex: 0001 | 1010 | 0011 β†’ 1, A, 3 β†’ 1A3
!Watch out

Always group from the right. If the left group is short, add 0s at its front. Keep the leading 0s when you change hex to binary, so each digit gives four bits.

Why hexadecimal is used

Computers do not use hex. Inside, everything is still binary. Hex is for people.

Hex is shorter: 8 bits become 2 hex digits. It is easier to read, remember and type.

People make fewer mistakes copying hex than long strings of 0s and 1s.

It converts to binary very easily, because each digit is one nibble.

UseReal exampleWhat the hex means
MAC address00:1A:2B:3C:4D:5EThe unique number of a network card. 12 hex digits instead of 48 bits.
HTML colour code#FF8800Red FF = 255, green 88 = 136, blue 00 = 0. This is orange.
Error codes0x80070005A Windows error code (access denied). The 0x means "hex follows".
URLsmy%20file.html%20 is hex 20 = denary 32, the code for a space.
Memory dumps3F A7 00 B6The contents of memory shown for a programmer finding a bug.
IPv6 addresses2001:0db8::7334Long network addresses written in hex groups.
!Watch out

Do not write "hex takes up less memory". It does not. The computer stores the same binary. Hex is only a shorter way for humans to write it.

Adding binary: the four rules

You add binary in columns, from right to left, like denary addition.

In binary, 1 + 1 makes 2, and 2 is written 10. So you write 0 and carry 1 to the next column.

If a column has 1 + 1 plus a carry of 1, the total is 3. Three is 11: write 1 and carry 1.

Try the toy: it starts with 91 + 54. Press Step to add one column at a time and watch where each carry goes. Then press Overflow example.

Column adds up toDenary totalWriteCarry
0 + 0000
0 + 1 or 1 + 0110
1 + 1201
1 + 1 + carry 1311

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

A full 8-bit addition

Write the two numbers one under the other, with the column values on top.

Write each carry above the next column on the left. Then add that column, including the carry.

Check your answer by changing everything to denary.

1286432168421
Carry11111
10901101101
+ 5400110110
= 16310100011
e.g.Example
Column 1: 1 + 0 = 1 Column 2: 0 + 1 = 1 Column 4: 1 + 1 = 10 β†’ write 0, carry 1 Column 8: 1 + 0 + carry 1 = 10 β†’ write 0, carry 1 Column 16: 0 + 1 + carry 1 = 10 β†’ write 0, carry 1 Column 32: 1 + 1 + carry 1 = 11 β†’ write 1, carry 1 Column 64: 1 + 0 + carry 1 = 10 β†’ write 0, carry 1 Column 128: 0 + 0 + carry 1 = 1 Answer: 10100011 Check: 128 + 32 + 2 + 1 = 163, and 109 + 54 = 163
!Watch out

Show your carries in the exam. A wrong answer with clear carries can still earn a working mark.

Overflow

An 8-bit register can hold 0 to 255. Sometimes an addition gives more than 255.

Then the last column makes a carry that has nowhere to go. It would need a 9th bit.

This is called an overflow error. The 9th bit is lost, so the stored answer is wrong.

The processor sets an overflow flag so the program can deal with it.

Lost1286432168421
Carry11
20011001000
+ 10001100100
Stored100101100
e.g.Example
200 + 100 = 300. But 300 is more than 255. The full answer needs 9 bits: 1 00101100. The register keeps only the right 8 bits: 00101100 = 32 + 8 + 4 = 44. 300 βˆ’ 256 = 44, because the lost 9th bit was worth 256.
?Why?

A register has a fixed number of bits. Anything that does not fit is lost, so the computer cannot store a value above 2ⁿ βˆ’ 1.

Logical left shift

A logical shift moves every bit left or right by a number of places.

In a left shift, bits fall off the left end and are lost. 0s fill the gaps on the right.

Each place shifted left multiplies the number by 2. Two places is Γ— 4. Three places is Γ— 8.

If a 1 falls off the left end, the answer is wrong. This is a kind of overflow.

Try the toy: load 22 (00010110), choose 2 places and press Shift left. Then load 53 (00110101) and shift it left 3 places. Which answer is wrong?

1286432168421Denary
Before0001011022
Left 20101100088
e.g.Example
22 shifted left 2 places: 00010110 β†’ 01011000 0 and 0 fall off the left. Two 0s come in on the right. 01011000 = 64 + 16 + 8 = 88. And 22 Γ— 4 = 88. Correct. 53 shifted left 3 places: 00110101 β†’ 10101000 0, 0 and 1 fall off the left. The lost 1 was important. 10101000 = 128 + 32 + 8 = 168. But 53 Γ— 8 = 424. Wrong answer, because 424 is too big for 8 bits.
?Why?

Moving a bit one column left puts it in a column worth twice as much. So the whole number doubles.

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

Logical right shift

In a right shift, bits fall off the right end and are lost. 0s fill the gaps on the left.

Each place shifted right divides the number by 2. Two places is Γ· 4. Three places is Γ· 8.

If a 1 falls off the right end, the remainder is lost. The answer is rounded down, so accuracy is lost.

1286432168421Denary
Before0001011022
Right 2000001015
e.g.Example
180 shifted right 1 place: 10110100 β†’ 01011010 = 64 + 16 + 8 + 2 = 90. And 180 Γ· 2 = 90. Exact. 22 shifted right 2 places: 00010110 β†’ 00000101 = 4 + 1 = 5 But 22 Γ· 4 = 5.5. The bits 1 and 0 fell off the right end. The lost 1 was worth 0.5 after dividing, so the answer is not exact.
!Watch out

In a logical shift the new bits are always 0. Do not copy the old end bit back in, and do not wrap bits round to the other end.

Two's complement: negative numbers

So far 8 bits only held positive numbers, 0 to 255. Two's complement is a way to store negative numbers too.

The only change: the leftmost column is worth βˆ’128 instead of 128. All other columns stay the same.

If the leftmost bit is 0, the number is positive. If it is 1, the number is negative.

8-bit two's complement holds βˆ’128 to +127.

Try the toy: press Smallest (βˆ’128) so only the βˆ’128 bit is on. Then tap the 64 bit. Watch the value climb towards 0.

βˆ’1286432168421Denary
0100110164 + 8 + 4 + 1 = 77
10110110βˆ’128 + 32 + 16 + 4 + 2 = βˆ’74
11111111βˆ’128 + 127 = βˆ’1
e.g.Example
Two's complement 10110110 to denary. The 1s are under βˆ’128, 32, 16, 4 and 2. βˆ’128 + 32 + 16 + 4 + 2 = βˆ’128 + 54 = βˆ’74
?Why?

With a βˆ’128 column, normal binary addition still works for negative numbers. The processor needs only one adding circuit.

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

Denary to two's complement

Positive number: write it in normal 8-bit binary. The leftmost bit will be 0.

Negative number, method 1: write the positive version in binary. Flip every bit (0 becomes 1, 1 becomes 0). Then add 1.

Negative number, method 2: start with βˆ’128. Work out what you must add to reach your number. Put that in the other 7 columns.

Use one method and check with the other, or check by adding the column values.

e.g.Example
Change βˆ’45 into 8-bit two's complement. Method 1 (flip and add 1): +45 = 00101101 Flip every bit: 11010010 Add 1: 11010011 Method 2 (start at βˆ’128): βˆ’45 = βˆ’128 + 83 83 = 64 + 16 + 2 + 1 So: 1 in the βˆ’128 column, then 1010011 Answer: 11010011 Check: βˆ’128 + 64 + 16 + 2 + 1 = βˆ’45
!Watch out

+45 and βˆ’45 are not the same pattern with a different first bit. 10101101 is βˆ’83, not βˆ’45. Always flip and add 1, or use the βˆ’128 method.

Study it

Open the study page and go one step at a time.

The steps for this lesson

Check yourself

One question at a time. The reason shows after each answer.

Saved on this device.