0% found this document useful (0 votes)
67 views54 pages

2 - Data Representation

Chapter 2 of the Ultimate GCSE CS textbook covers data representation, including units of information such as bits, bytes, and nibbles, as well as number bases like denary, binary, and hexadecimal. It explains conversions between these bases, binary arithmetic, character sets, and how data is represented in images and sound. Additionally, the chapter discusses data compression and the importance of these concepts in computer science.

Uploaded by

kacperoscalt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views54 pages

2 - Data Representation

Chapter 2 of the Ultimate GCSE CS textbook covers data representation, including units of information such as bits, bytes, and nibbles, as well as number bases like denary, binary, and hexadecimal. It explains conversions between these bases, binary arithmetic, character sets, and how data is represented in images and sound. Additionally, the chapter discusses data compression and the importance of these concepts in computer science.

Uploaded by

kacperoscalt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.

net

2 – Data Representation
Contents

1) Units of information ..................................................................................................... 2


Bits and bytes .......................................................................................................... 2
Nibbles ..................................................................................................................... 3
Quantities of bytes .................................................................................................. 3
2) Number bases ............................................................................................................. 5
Denary (base 10) .................................................................................................... 5
Binary (base 2) ........................................................................................................ 6
Hexadecimal (base 16).......................................................................................... 8
3) Converting between number bases ...................................................................... 10
Binary to denary conversion ................................................................................ 10
Hexadecimal to denary conversion ................................................................... 11
Binary to hexadecimal conversion ..................................................................... 13
Denary to binary conversion ............................................................................... 14
Hexadecimal to binary conversion ..................................................................... 17
Denary to hexadecimal conversion ................................................................... 17
4) Binary arithmetic ....................................................................................................... 21
Adding binary numbers ....................................................................................... 21
Overflow errors ...................................................................................................... 24
Binary shifts............................................................................................................. 25
Binary multiplication ................................................................................... 25
Binary division ............................................................................................. 27
5) Character sets ........................................................................................................... 30
ASCII ....................................................................................................................... 30
Sequential codes ....................................................................................... 31
Unicode ................................................................................................................. 32
Relationship to number of bits ............................................................................. 33
6) Representing images................................................................................................ 34
Bitmaps .................................................................................................................. 35
Bitmap size .................................................................................................. 35
Colour depth .............................................................................................. 37
Representing black and white bitmaps ............................................................. 38
Metadata .............................................................................................................. 39
Bitmap file sizes...................................................................................................... 40
Effect on file size ......................................................................................... 40
Calculating a bitmap file size ................................................................... 41
7) Representing sound .................................................................................................. 44
Sampling ................................................................................................................ 44
Storing sound files ................................................................................................. 46
8) Data compression .................................................................................................... 51
The need for compression ................................................................................... 51
Types of compression ........................................................................................... 51

© paullong.net 2020 Page 1 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

1) Units of information

In this section you will learn about bits, nibbles, bytes, kilobytes, megabytes,
gigabytes, terabytes and petabytes.

Bits and bytes


Computers use electrical signals which can be either on or off. On is represented
by the number 1 (one) and off is represented by the number zero (0). These are
known as bits. A bit is the smallest unit of data that a computer can use.

Example – set of bits


A set of bits known as a bit pattern. Here is an example of a bit pattern:
1010001101101001

When bits are joined together as a bit pattern, they can represent data or
instructions.

A byte is a group of 8 bits. A byte is the smallest bit pattern that can be used to
represent data in a computer system.

Example – byte
Here is a byte:
10100010

Bytes have an abbreviation of B.


Bits have an abbreviation of b.

Example – bits and bytes


5 b = 5 bits
8 b = 8 bits = 1 B = 1 byte
3 B = 3 bytes

Activity – bits and bytes


How many bits are there in:
1) 3 bytes 2) 7 bytes 3) 15 bytes 4) 24 bytes

How many bytes are there in:


5) 32 bits 6) 72 bits 7) 96 bits 8) 160 bits

© paullong.net 2020 Page 2 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Nibbles
A nibble is 4 bits or half a byte. Nibbles are useful when representing numbers
using hexadecimal because each hexadecimal digit represents a nibble. Two
hexadecimal digits together represent a byte.

Quantities of bytes
Measurements are often recorded using the International System of Units, known
as SI units. Multiples of 1,000 bytes can be represented using SI units as follows:

1 kilobyte = 1 kB = 1,000 B = 1,000 bytes = 103 bytes


1 megabyte = 1 MB = 1,000 kB = 1,000,000 bytes = 106 bytes
1 gigabyte = 1 GB = 1,000 MB = 1,000,000,000 bytes = 109 bytes
1 terabyte = 1 TB = 1,000 GB = 1,000,000,000,000 bytes = 1012 bytes
1 petabyte = 1 PB = 1,000 TB = 1,000,000,000,000,000 bytes = 1015 bytes

Maths Link – kilos


1 kilogram = 1 kg = 1,000 grams
1 kilometre = 1 km = 1,000 metres

Activity – quantities of bytes


1) How many bytes are there in:
a) 8 kB b) 463 kB c) 200 kB d) 23 MB e) 650 GB f) 702 TB g) 3 PB

2) How many kilobytes are there in:


a) 2 MB b) 30 MB c) 8 GB d) 705 GB e) 17 TB f) 0.5 TB g) 0.02 PB

3) How many gigabytes are there in:


a) 42 TB b) 390 TB c) 0.5 TB d) 600 MB e) 51 PB

Maths Link – powers of 2


1 kilobyte used to be 1,024 bytes which is also 210 bytes

To avoid confusion, this is now known as a kibibyte (KiB).

Similarly, 1,024 KiB which is also 220 bytes is known as a mebibyte.


1,024 MiB which is also 230 bytes is known as a gibibyte.
1,024 GiB which is also 240 bytes is known as a tebibyte.

This is for information only. You do not need to learn these terms.

© paullong.net 2020 Page 3 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Questions – follow me
1) Explain why computers use bits as a unit of data. [2]

2) Describe a byte. [1]

3) What is the SI unit for 1,000,000,000 (billion) bytes? [1]

4) A computer has 2 terabytes of hard disk space and 4 megabytes of RAM.

a) How many bytes are there on the hard disk? [1]


b) How many bytes are there in the RAM? [1]

5) Put the following units in order from smallest to largest:

nibble MB GB PB bit TB byte kB [1]

© paullong.net 2020 Page 4 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

2) Number bases

A number base defines how many unique digits are used within a number system.
Denary (base 10) has 10 unique digits, binary (base 2) has 2 unique digits and
hexadecimal (base 16) has 16 unique digits.

Denary (base 10)


The number system that we are all used to in mathematics is denary (base 10, also
known as decimal) which has 10 unique digits:

0 1 2 3 4 5 6 7 8 9

A single digit number will only use one of the digits from zero to nine. When we use
a combination of more than one digit, we start to multiply by 10, 100, 1000 etc.

Example – denary digits


57 = 5 x 10 839 = 8 x 100
+7x1 + 3 x 10
+9x1

Maths Link – powers of 10


The denary system is based on powers of 10.

101 means 10 to the power of 1 which means multiply by 10 once. 5 x 101 = 50


102 means 10 to the power of 2 which means multiply by 10 twice. 5 x 102 = 500

5 x 103 = 5 000 5 x 104 = 50 000 5 x 105 = 500 000

Notice how the power represents the number of zeros in the denary number.

100 means 10 to the power of zero which means multiply by 1 only. 5 x 100 = 5

From the example above, 839 can be represented as:

839 = 8 x 102
+ 3 x 101
+ 9 x 100

© paullong.net 2020 Page 5 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

If you start from 0 (zero) then the power represents the position in the number:

Number 8 3 9 (839)
Power 2 1 0
8 x 102 3 x 101 9 x 100 (800 + 30 + 9)
Multiple 100s 10s Units (1s)
8 x 100 3 x 10 9x1 (800 + 30 + 9)

Example – denary tables


A denary number can be calculated by using the table below to multiply each
digit by the correct multiple.

5 2 3 7
x x x x
1000 100 10 1
5000 + 200 + 30 + 7 = 5 237

Activity – denary tables


Complete the gaps in the denary tables activity sheet.

Binary (base 2)

Video
Watch http://tiny.cc/whybinary from code.org about why computers use binary.

The number system used by computers is binary (base 2). This is because there are
only two unique digits which reduces the chances of data corruption:

0 and 1

Each digit in binary is known as a bit – a binary digit. Computers use electrical
switches that can either be on or off. On is represented by 1 (one) and off is
represented by 0 (zero). Bits can be combined to form a bit pattern. All data in a
computer is represented using bit patterns; a bit pattern could represent many
different things including an image, a sound, text or a number. A bit pattern that
represents a number is called a binary number.

© paullong.net 2020 Page 6 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Maths Link – powers of 2


The binary system is based on powers of 2.

21 means 2 to the power of 1 which means multiply by 2 once. 1 x 21 = 2


22 means 2 to the power of 2 which means multiply by 2 twice. 1 x 22 = 4

1 x 23 = 8 1 x 24 = 16 1 x 25 = 32 1 x 26 = 64 1 x 27 = 128

20 means 2 to the power of zero which means multiply by 1 only. 1 x 20 = 1

Here is the binary line which shows the powers of 2 and the denary values:

Power 27 26 25 24 23 22 21 20
Denary 128 64 32 16 8 4 2 1

1 0 1 1 can be represented as: 1 0 1 1

1 0 1 1 = 1 x 23
+ 0 x 22
+ 1 x 21
+ 1 x 20

If you start from 0 (zero) then the power represents the position in the number:

Number 1 0 1 1 (1011)
Power 3 2 1 0
1 x 23 0 x 22 1 x 21 1 x 20 (8 + 0 + 2 + 1)
Multiple 8 4 2 1
1x8 0x4 1x2 1 x 1 (8 + 0 + 2 + 1)

Denary equivalent = 8 + 0 + 2 + 1 = 11

Example – binary numbers


1011=9
0 1 1 1 1 0 1 1 = 123
1 1 1 1 = 15
0 1 0 0 0 0 0 0 1 0 0 1 0 1 = 4133

Video
Watch http://tiny.cc/khannumbers from Khan Academy about number bases.

© paullong.net 2020 Page 7 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Hexadecimal (base 16)


Computers use long bit patterns. These can be very difficult for people to read or
understand. In order to simplify this, computer scientists often use the hexadecimal
(base 16) number system to represent long bit patterns.

The 16 hexadecimal digits are:

0 1 2 3 4 5 6 7 8 9 A B C D E F

Each hexadecimal digit can be used to represent a 4-bit binary number (nibble).
The table below shows the equivalent denary, binary and hexadecimal values:

Denary Binary Hexadecimal


0 0000 0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F

This makes it much easier to represent long bit patterns.

Example – bit patterns as hexadecimal values


1111=F
1 0 0 1 1 0 0 0 = 9 8 (spoken as nine eight and NOT ninety eight)
0111 1010=7A
0100 0000 1001 1110=409E

You will often see hexadecimal values used to represent colours. Each pair of
hexadecimal digits represents each colour RED, GREEN and BLUE which each
have values from 0 to 255 or 0 to FF. This is just one way of using hexadecimal.

© paullong.net 2020 Page 8 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Activity – hexadecimal digits


Identify the hexadecimal values equivalent to these binary numbers:
1) 1011
2) 0011
3) 0010 1011
4) 0100 0110 1111 1100

Video
Watch http://tiny.cc/khanhex from Khan Academy.

Questions – follow me
1) State the names of the number systems of each of the following number bases:
a) Base 2 [3]
b) Base 10
c) Base 16

2) State the name of the number systems used in each of the following numbers
(some can have more than one answer): [5]
a) F4
b) 94
c) 1010
d) 11
e) 3B

3) Use the table on page 4 to lookup the hexadecimal values for each of the
following binary bit patterns: [3]
a) 1011
b) 0000
c) 1110

4) Explain why computers use the binary number system instead of denary. [2]

5) Explain why the hexadecimal number system is used in computer science. [2]

© paullong.net 2020 Page 9 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

3) Converting between number bases

Each binary and hexadecimal number has a denary equivalent. It is therefore


possible to convert between each number base. For OCR GCSE Computer
Science the maximum values you will be tested on are:

Denary: 255
Binary: 1111 1111
Hexadecimal: FF

Binary to denary conversion


Use the binary line to calculate the denary equivalent of a binary number:

Power 27 26 25 24 23 22 21 20
Denary 128 64 32 16 8 4 2 1

Tip: Write out the binary line as soon as your exam starts. All you need to
remember is that 20 = 1 and 27 = 128. You can then fill in the gaps.

Example – binary nibble tables


A 4-bit binary number (nibble) can be converted to a denary number by using the
table below to multiply each digit by the correct multiple.

1 0 1 1
x x x x
8 4 2 1
8 + 0 + 2 + 1 = 11

Example – binary byte tables


An 8-bit binary number (byte) can be converted to a denary number by using the
table below to multiply each digit by the correct multiple.

1 1 0 1 1 0 1 1
x x x x x x x x
128 64 32 16 8 4 2 1
128 + 64 + 0 + 16 + 8 + 0 + 2 + 1 = 219

© paullong.net 2020 Page 10 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Activity – binary tables


Complete the gaps in the binary tables activity sheet.

Check your answers using this spreadsheet created by Stuart Davison.

Use the random number generator to convert more binary numbers to denary.

Extension: create your own spreadsheet, or computer program using a


programming language you have learnt, to convert binary to denary
without using pre-defined functions for this purpose.

Hexadecimal to denary conversion


Remember that hexadecimal (base 16) has 16 digits:

0 1 2 3 4 5 6 7 8 9 A B C D E F

When converting to denary, the first 10 digits (0 – 9) are the same as hexadecimal:

Hexadecimal 0 1 2 3 4 5 6 7 8 9
Denary 0 1 2 3 4 5 6 7 8 9

After that, it is a case of remembering what comes next:

Hexadecimal A B C D E F
Denary 10 11 12 13 14 15

To convert a single digit hexadecimal (nibble) to denary is quite easy if the


hexadecimal value is between 0 and 9. It is then simply a case of remembering
that A is 10 up to F which is 15.

Example – single digit hexadecimal to decimal conversion


Hex 5 = Denary 5
Hex 8 = Denary 8
Hex B = (remember A is 10) Denary 11
Hex F = Decimal 15

Maths Link – powers of 16


The hexadecimal system is based on powers of 16.

161 means 16 to the power of 1 which means multiply by 16 once. 1 x 161 = 16


162 means 16 to the power of 2 which means multiply by 16 twice. 1 x 162 = 256

© paullong.net 2020 Page 11 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

160 means 16 to the power of zero which means multiply by 1 only. 1 x 160 = 1

A two digit hexadecimal number can be represented as:

8B = 8 x 161
+ B x 160

If you start from 0 (zero) then the power represents the position in the number:

Number 8 B
Power 161 160
8 x 161 B x 160 (8 x 16) + (8 x 1)
Multiple 16 1
8 x 16 Bx1 (128 + 11)

Denary equivalent = 128 + 11 = 139

Extension: What would the table above look like for the hexadecimal value 7E4A?

Maths Link – learn your 16 times table


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 256

Tip: if you know your 8 times table, then you already know your 16 times table –
just double everything – e.g. 4 x 8 = 32 so 4 x 16 = 64. Alternatively write out
the table and fill in 1 x 16 = 16, 10 x 16 = 160. You then know that 5 x 16 = 80
(half of 10 x 16) and you can now fill in 15 x 16 (160 + 80). Now fill in the gaps.

Example – hexadecimal tables


A 2-digit hexadecimal number (byte) can be converted to a denary number by
using the table below to multiply each digit by the correct multiple.

Hex digits: 4 F
Denary digits: 4 15
x x
16 + 1
64 + 15 = 79

© paullong.net 2020 Page 12 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Activity – hexadecimal tables


Complete the gaps in the hexadecimal tables activity sheet.

Use the random number generator to convert more hex numbers to denary.

Note: it is also possible to convert hexadecimal to binary and then to denary.

Binary to hexadecimal conversion


Remember that each 4-bit pattern (nibble) represents a single hexadecimal digit.
To start with we will focus on just a single 4-bit pattern and convert it to denary:

1 1 0 1
x x x x
8 4 2 1
8 + 4 + 0 + 1 = 13

Now we have the denary value, we can convert this to hexadecimal by recalling
the tables we used previously:

Hexadecimal 0 1 2 3 4 5 6 7 8 9
Denary 0 1 2 3 4 5 6 7 8 9

Hexadecimal A B C D E F
Denary 10 11 12 13 14 15

Therefore, as the denary value of 1101 is 13, the hexadecimal value is D.


Next, we need to look at longer bit patterns up to 8-bit. This includes 2 sets of 4-bit
patterns.

Example – 8-bit pattern


1101 1001 (2 nibbles)

Each 4-bit pattern can be separately represented by a hexadecimal digit.


Therefore, the process is to convert each 4-bit pattern separately into a
hexadecimal digit.

Example – 8-bit pattern hexadecimal value


1101 1001

First nibble: 1 1 0 1 in denary is 8 + 4 + 0 + 1 = 13. In hexadecimal, 13 is D.

Second nibble: 1 0 0 1 in denary is 8 + 0 + 0 + 1 = 9. In hexadecimal, 9 is 9.

1101 1001 in hexadecimal is D9


© paullong.net 2020 Page 13 of 54 by Paul Long
The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Activity – binary to hexadecimal


Convert these binary numbers to hexadecimal values:

1) 1001
2) 1111
3) 0110 1110
4) 0101 0111
5) 0010 0000

Use the random number generator to convert more binary numbers to hex.

Denary to binary conversion


First of all, we will start by using denary values up to 15 that use a 4-bit pattern.
Remember the table used to convert binary to denary:

1 1 1 1
x x x x
8 4 2 1
8 + 4 + 2 + 1 = 15

The maximum denary value for a 4-bit pattern is 15. To convert a 4-bit pattern to a
denary number, you need to calculate how many 8s, 4s, 2s and 1s there are in the
denary value. You need to divide by 8, then use the remainder to divide by 4,
then use the remainder to divide by 2 and then the remainder to divide by 1.

Example – denary to 4-bit pattern conversion


Convert 14 to a 4-bit pattern binary number.

14 ÷ 8 = 1 remainder 6

6 ÷ 4 = 1 remainder 2

2 ÷ 2 = 1 remainder 0

0÷1=0

The 4-bit pattern is therefore 1 1 1 0.

© paullong.net 2020 Page 14 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

A table can be used to help with this method. Below, the original denary number
is divided by 8. The answer is 1 remainder 6. The remainder is then put into the
next column and divided by 4 and this continues.

14 6 2 0
÷ ÷ ÷ ÷
8 4 2 1
1 r6 1 r2 1 r0 0 = 1110

Video
Watch http://tiny.cc/denary2binary from Khan Academy.

Activity – denary to 4-bit pattern


Complete the gaps in the denary to 4-bit pattern activity sheet.

Video
Watch http://tiny.cc/largedenarys from Khan Academy and notice how much
more complicated it is to keep dividing by bigger numbers.

As denary values become bigger, this can become rather complicated as you will
have to divide by bigger values such as 32, 64 and 128. There is a much simpler
method which involves repeatedly dividing the denary number by two until it can’t
be divided anymore.

Example – denary divide by 2 method


Convert 14 to a 4-bit pattern binary number.

Divide 14 by 2 and you will get a dividend of 7 with a remainder of 0. Keep the
remainder as part of the binary answer and divide the dividend by 2. Keep doing
this until you get a dividend of 0.

14 ÷ 2 = 7 remainder 0 Read the binary


7 ÷ 2 = 3 remainder 1 number from bottom
3 ÷ 2 = 1 remainder 1 to top and that is your
1 ÷ 2 = 0 remainder 1 answer – 1 1 1 0.

Dividend

© paullong.net 2020 Page 15 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

This can be done with any denary number, including larger denary numbers.

Example – denary divide by 2 method


Convert 221 to a binary number.

221 ÷ 2 = 110 remainder 1


110 ÷ 2 = 55 remainder 0
55 ÷ 2 = 27 remainder 1 Read the binary
27 ÷ 2 = 13 remainder 1 number from bottom
to top and that is your
13 ÷ 2 = 6 remainder 1
answer –
6 ÷ 2 = 3 remainder 0 1 1 0 1 1 1 0 1.
3 ÷ 2 = 1 remainder 1
1 ÷ 2 = 0 remainder 1

Maths Link – 8-bit binary number


Binary numbers are often written using multiples of 4 bits. Therefore, if you have less
than 8 bits you can put zeros in front.

1 0 1 0 1 1 is the same as 0 0 1 0 1 0 1 1.

You could imagine this being the same for denary numbers as 384 is the same as
00384.

Activity – denary to binary


Convert the following denary numbers to binary:

1) 5 2) 9 3) 13 4) 15
5) 25 6) 39 7) 58 8) 16
9) 77 10) 135 11) 188 12) 201
13) 128 14) 127 15) 239 16) 255

Activity – binary fun


Play this binary fun game and the Cisco binary game from Penjee at
http://tiny.cc/BinaryNumbersGame

© paullong.net 2020 Page 16 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Hexadecimal to binary conversion


Each hexadecimal digit represents a nibble. A nibble is half a byte which is 4
binary digits. Therefore, all you need to do is to convert each hexadecimal digit to
denary and then each denary value to binary.

Example – hexadecimal to binary (one nibble at a time)


Convert F2 to a binary number.

F in hexadecimal = 15 in denary 2 in hexadecimal = 2 in denary

Convert 15 to binary: Convert 2 to binary:


15 = 8 + 4 + 2 + 1 2=0+0+2+0
Binary = 1 1 1 1 Binary = 0 0 1 0

Combine the two nibbles to make a byte:

11110010

Activity – hexadecimal to binary


Convert the following hexadecimal numbers to binary:

1) 5C 2) 3E 3) 25 4) 90
5) A4 6) F9 7) 00 8) E7

Play the Flippy Bit game at www.tiny.cc/flippybit

Denary to hexadecimal conversion


For denary numbers up to 15, it is simply a case of remembering the table:

Denary 0 1 2 3 4 5 6 7 8 9
Hexadecimal 0 1 2 3 4 5 6 7 8 9

Denary 10 11 12 13 14 15
Hexadecimal A B C D E F

Example – denary to single hexadecimal digit


Convert 12 from denary to hexadecimal.
Look up in the table above and the answer is C.

© paullong.net 2020 Page 17 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Denary numbers bigger than 15 are slightly more complicated. One way of doing
this is to convert the denary value to binary and then convert the binary value to
hexadecimal.

A quicker way to solve this problem is to remember what base 16 means.

Maths Link – powers of 16


A two-digit hexadecimal number can be represented as:

8B = 8 x 161
+ B x 160

When converting from hexadecimal to denary we multiply by powers of 16. To


convert from denary to hexadecimal we need to divide by powers of 16. You only
need to deal with 2 hexadecimal digits at GCSE level so the largest number you
need to divide by is 16.

The process is to divide the denary number by 16 and the dividend (answer
without remainder) is what will be used for the first hexadecimal digit. The
remainder will then be used for the second hexadecimal digit.

Example – denary to hexadecimal


Convert 235 to a hexadecimal number.

235 ÷ 16 = 14 remainder 11
(14 x 16 = 224. The remainder left over is 235 – 224 = 11)

14 is E in hexadecimal and 11 is B so the answer is EB

Maths Link – learn your 16 times table


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 256

Maths Link – remainders


To calculate the remainder, you can either use the long division method or you
can use a calculator to work it out.

First calculate 235 ÷ 16 to get an answer of 14.6875

© paullong.net 2020 Page 18 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Now use the whole number part of the answer (14) and multiply by 16:

The answer is 224.

Subtract this answer from the original number of 235 to find the remainder:

The answer (remainder) is 11

If you have a scientific calculator then you can calculate the modulus (MOD)
which is also the remainder. Enter 235, then press MOD then enter 16:

The answer is 11.

Note: you cannot use a calculator in the exam

Video
Watch http://tiny.cc/dec2hex from Khan Academy.

© paullong.net 2020 Page 19 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Activity – denary to hexadecimal


Convert the following denary numbers to hexadecimal:

1) 8 2) 10 3) 15 4) 13
5) 35 6) 176 7) 201 8) 255

Use the random number generator to convert more denary numbers to hex.

Questions – follow me
1) Convert the following binary numbers to denary numbers: [3]
a) 1 1 1 1
b) 0 1 0 1 0 1 0 1
c) 1 0 1 0 1 0 1 0

2) Convert the following hexadecimal numbers to denary numbers: [3]


a) C
b) 5D
c) E2

3) Convert the following binary numbers to hexadecimal numbers: [3]


a) 1 1 1 0
b) 0 1 1 1 1 1 0 1
c) 1 0 1 0 0 1 1 1

4) Convert the following denary numbers to binary numbers: [3]


a) 7
b) 35
c) 182

5) Convert the following hexadecimal numbers to binary numbers: [3]


a) E
b) 4F
c) B7

6) Convert the following denary numbers to hexadecimal numbers: [3]


a) 12
b) 47
c) 199

© paullong.net 2020 Page 20 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

4) Binary arithmetic

Adding binary numbers


Some binary numbers are easy to add up:

0+0=0

1+0=1

0+1=1

Example – simple binary addition


00100001
+ 01011100
01111101

Activity – adding binary numbers 1


Add the following binary numbers. Show your working.

1) 0 0 1 1
+ 0 1 0 0

2) 0 0 0 1
+ 1 0 0 0

3) 0 0 1 1 0 1 0 1
+ 1 0 0 0 0 0 1 0

4) 1 1 0 1 0 0 1 0
+ 0 0 1 0 1 1 0 1

However, 1 + 1 = 2. Two is not a binary number. It is actually 1 0 (one zero).


Remember this:

1 0
x x
2 1 Therefore, 1 + 1 is not that simple. It
requires a digit to be carried like in
1 + 0 = 2 denary arithmetic.

© paullong.net 2020 Page 21 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Maths Link – adding denary numbers


When you add denary numbers with more than one digit, you have to carry 10s,
100s, 1000s etc.

2 5 3 3 + 8 = 1 1.
+ 4 4 8
1 1 is written in the units column
1 and 1 is carried.

2 5 3 5 + 4 + 1 = 1 0.
+ 4 4 8
0 1 0 is written in the tens column and
1 1 1 is carried.

2 5 3 2 + 4 + 1 = 7.
+ 4 4 8
7 0 1 7 is written in the hundreds column.
1 1

When denary numbers are added, the maximum digit in a column is 9. The next
number is 10 (ten) and so 0 is put in the column with 1 (one) carried to the next
column.

Binary works the same way. The maximum digit in a column is 1. The next number
is 1 0 (one zero) and so if a column adds up to 1 0 (one zero) a 0 (zero) is put in the
column with 1 (one) carried to the next column.

Example – carrying binary numbers 1


1 0 1 1 + 1 = 1 0 (one zero, not ten, denary value 2).
+ 0 0 1
0 0 is written in the ones column
1 and 1 is carried.

1 0 1 0 + 0 + 1 = 1.
+ 0 0 1
1 0 1 is written in the twos column.
1

1 0 1 1+0=1
0 0 1
+ 1 1 0 1 is written in the fours column.

Check in denary: 5 + 1 = 6 (1 1 0 in binary)


© paullong.net 2020 Page 22 of 54 by Paul Long
The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Video
Watch http://tiny.cc/binaryadd from Khan Academy.

Example – carrying binary numbers 2


0 1 1 1 + 1 = 1 0 (one zero, not ten, denary value 2).
+ 1 1 1
0 0 is written in the ones column
1 and 1 is carried.

0 1 1 1 + 1 + 1 = 1 1 (one one, not eleven).


+ 1 1 1
1 0 1 is written in the twos column.
1 1 and 1 is carried

0 1 1 0 + 1 + 1 = 1 0 (one zero).
+ 1 1 1
1 0 1 0 0 is written in the fours column
1 1 and 1 is carried.

Check in denary: 3 + 7 = 10 (ten, 1 0 1 0 in binary)

Activity – adding binary numbers 2


Add the following binary numbers. Show your working.

1) 0 0 1 1 0 0 0 1
+ 0 0 1 0 0 1 1 0

2) 0 0 1 1 1 0 0 1
+ 1 0 1 1 0 1 0 1

3) 0 0 1 1 0 1 0 1
+ 1 0 1 1 0 1 1 1

4) 0 1 1 1 1 0 1 1
+ 0 0 1 0 1 1 0 1

5) 0 0 1 1 1 1 1 1
+ 1 0 1 1 1 1 1 1

Extension: Use the adding binary numbers 3 worksheet to add 3 binary numbers.
Extension: Use the random number generator to add more binary numbers.
You can check your answers by converting the binary numbers to denary.
© paullong.net 2020 Page 23 of 54 by Paul Long
The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Overflow errors
Imagine you had a field in a database that stored the year in two-digit format (eg
18 instead of 2018). Imagine what would happen when adding one to the year
99:

99 + 1 = ?
Is the answer 100, 00 or something else?

Example – millennium bug


Before the year 2000, many computer systems used 2 digit dates (eg 94 for 1994).
Every new year, the year is increased by one. But 99 plus one equals 100 which
has 3 digits. This was commonly known as the ‘millennium bug’ which caused
some computers to crash.

A similar problem happens when adding numbers in binary. The largest number
that can be represented by a byte is 255. If only one byte is used for storing
numbers, then an overflow error can occur if binary numbers are added together
resulting in a total bigger than 255. Overflow occurs if there is a carry from the MSB.

Power 27 26 25 24 23 22 21 20
Denary 128 64 32 16 8 4 2 1

MSB = most significant bit (the bit that represents the largest value)
LSB = least significant bit (the bit that represents the smallest value – one)
The MSB is always on the left and the LSB is always on the right.

Example – overflow error


0 1 1 1 1 1 1 1 127 in denary
+ 1 1 0 0 0 0 0 0 192 in denary
1 1 1 1 1 1 So far everything is OK

0 1 1 1 1 1 1 1 1 + 1 = 1 0 (one zero)
+ 1 1 0 0 0 0 0 0
0 1 1 1 1 1 1 0 is written in the column and 1 carried
1

0 1 1 1 1 1 1 1 0 + 1 + 1 = 1 0 (one zero)
+ 1 1 0 0 0 0 0 0
0 0 1 1 1 1 1 1 0 is written in the column and 1 carried
1 1

Now there is a problem. The answer needs to include the 1 that was carried from
the MSB. However, this would require a 9th bit. As only a byte has been allocated,
only 8 bits are available and so there is an overflow error. A value of 256 is missing
from the answer in this example.
© paullong.net 2020 Page 24 of 54 by Paul Long
The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Binary shifts
Binary shifts are used for multiplication and division.

Binary multiplication

Maths Link – multiplying denary by 10, 100, 1000


To multiply an integer by 10, you shift the number to the left and put a zero in its
place:
5 x 10 = 50

To multiply an integer by 100, you shift the number twice to the left and put 2 zeros
at the end:
5 x 100 = 500

To multiply an integer by 10000, you shift the number 3 places to the left and put 3
zeros at the end:
5 x 1000 = 5000

In denary (base 10), you would shift to the left and add zeros when multiplying
integers by 10, 100, 1000.

In binary (base 2), you shift to the left and add zeros when multiplying by 2, 4, 8.
This only applies to binary integers.

Example – multiplying binary by 2, 4, 8


To multiply a binary number by 2, put a zero at the end of the number:

11x2=110 (check: 3 x 2 = 6)

To multiply a binary number by 4, put two zeros at the end of the number:

11x4=1100 (check: 3 x 4 = 12)

To multiply a binary number by 8, put three zeros at the end of the number:

11x4=11000 (check: 3 x 8 = 24)

This is known as the binary shift left because all bits are moved one place to the
left.

© paullong.net 2020 Page 25 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Example – binary shift left


To multiply a binary number by 2, shift 1 place to the left:
Denary 128 64 32 16 8 4 2 1
Original 0 0 0 1 0 0 0 1
x2 0 0 1 0 0 0 1 0

Fill the gap on the right with a zero. The zero at the left is lost.

Check: 17 x 2 = 34 32 + 2 = 34

To multiply a binary number by 4, shift 2 places to the left:


Denary 128 64 32 16 8 4 2 1
Original 0 0 0 1 0 0 0 1
x4 0 1 0 0 0 1 0 0

Fill the gaps on the right with zeros. The zeros at the left are lost.

Check: 17 x 4 = 68 64 + 4 = 68

To multiply a binary number by 8, shift 3 places to the left:


Denary 128 64 32 16 8 4 2 1
Original 0 0 0 1 0 0 0 1
x8 1 0 0 0 1 0 0 0

Fill the gaps on the right with zeros. The zeros at the left are lost.

Check: 17 x 8 = 136 128 + 8 = 136

The general rule for a left shift, is add a zero to the right side of the binary number
(just after the LSB) and discard the bit at the left most side of the binary number.
However, this should not be done if the MSB (most significant bit) is a 1 (one) and
there are a limited number of bits available (e.g. one byte) because it would
cause an overflow error.

Example – overflow error


The multiplication by 2 below causes an overflow error:
Denary 128 64 32 16 8 4 2 1
Original 1 0 0 0 1 0 0 0
x2 0 0 0 1 0 0 0 0

Check: 136 x 2 ≠ 16 it should be 272

There are only 8-bits available. What would have been the new MSB representing
256 cannot be fitted into the 8-bit answer.

© paullong.net 2020 Page 26 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Activity – binary shift left


Calculate the following. The first number in each question is shown in binary and
the second number is shown in decimal.

1) 0110 x 4 2) 1100 x 8 3) 0101 x 2 4) 1000 x 16


5) 0001 0010 x 8 6) 0101 1110 x 2

Extension:
Each answer should be shown in 8-bit binary format (one byte).

7) 0111 1101 x 4 8) 1011 1011 x 8


9) Explain the problem with your answers to questions 7 and 8.

The binary line shows us how many places we need to shift by looking at the power
of 2:
Places to shift 27 26 25 24 23 22 21 20
Multiplier 128 64 32 16 8 4 2 1
For example, to multiply or divide by 32, you would shift 5 places.

multiply or divide by 2n = shift n places


Binary division
The opposite rule applies for a right shift which is used for division. Add a zero to
the left side of the binary number (just before the MSB) and discard the bit at the
right most side of the binary number. This only works for the whole number
(integer) part of the answer, so it is an integer division (DIV) rather than a regular
arithmetic division because anything after the decimal point is lost.

Example – logical binary shift right


To divide a binary number by 2, shift 1 place to the right:
Denary 128 64 32 16 8 4 2 1
Original 0 0 1 0 0 0 1 0
÷2 0 0 0 1 0 0 0 1
Fill the gap on the left with a zero. The zero at the right is lost.

Check: 34 ÷ 2 = 17 16 + 1 = 17

To divide a binary number by 4, shift 2 places to the right:


Denary 128 64 32 16 8 4 2 1
Original 0 0 1 0 0 0 1 0
÷4 0 0 0 0 1 0 0 0
Fill the gaps on the left with a zero. The one and zero at the right are lost.

Check: 34 ÷ 4 = 8.5 ≠ 8 BUT 34 DIV 4 = 8 (integer division is OK)


© paullong.net 2020 Page 27 of 54 by Paul Long
The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

To divide a binary number by 8, shift 3 places to the right:


Denary 128 64 32 16 8 4 2 1
Original 0 0 1 0 0 0 1 0
÷8 0 0 0 0 0 1 0 0
Fill the gaps on the left with a zero. The zero, one and zero at the right are lost.

Check: 34 ÷ 8 = 4.25 ≠ 8 BUT 34 DIV 8 = 4 (integer division is OK)

Note: a binary shift right is an integer/floor division (DIV) rather than a regular
arithmetic division.

Activity – binary shift right


Calculate the following. The first number in each question is shown in binary and
the second number is shown in decimal.

1) 0110 ÷ 2 2) 1100 ÷ 4 3) 0100 ÷ 2 4) 1000 ÷ 8


5) 0011 1000 ÷ 8 6) 0101 1110 ÷ 2

Extension:
7) 0001 0011 ÷ 2 8) 1011 1010 ÷ 8

9) Explain the problem with your answers to questions 7 and 8.

Video
Watch http://tiny.cc/binaryshift from CatComputerTeacher on YouTube.

© paullong.net 2020 Page 28 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Questions – follow me
1) State the purpose of a binary shift right? [1]

2) State the purpose of a binary shift left? [1]

3) State why might you perform a binary shift of 3 places to the left. [1]

4) State why might you perform a binary shift of 2 places to the right. [1]

5) a) Add together the following two 8-bit binary numbers. Express your
response in an 8-bit binary form. [2]

0 1 1 0 0 0 1 1
+ 1 0 1 1 1 1 0 0

b) State the problem this addition has created. [1]


c) Explain why this is a problem. [2]

5) a) Perform a two place right shift on the binary number 11001010. [1]
b) Explain the effect of performing this right shift. [2]

© paullong.net 2020 Page 29 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

5) Character sets

Computers only work with binary bits. Therefore, all characters need to be
represented by a binary number. A character set is the set of possible characters
that can be represented by a computer system.

ASCII
The American Standard Code for Information Interchange (ASCII) is a set of bit
patterns used for standard characters and control codes. In order that all
applications can understand the data being used, a standard set of character
codes must be used.

Example – ASCII character codes


Character Denary Number Binary Number
[Null char] 0 0000 0000
[Horizontal Tab] 9 0000 1001
# 35 0010 0011
+ 43 0010 1011
5 53 0011 0101
A 65 0100 0001
Z 90 0101 1010
a 97 0110 0001
z 122 0111 1011
[Delete] 127 0111 1111

7-bit ASCII is the original character set that was adopted internationally and only
uses 7 bits. This means the number of unique codes is limited to 128 and 32 of
these are control codes which means only 96 characters. This was then extended
to 8-bit ASCII to allow for foreign characters. Therefore, there are 256 codes from 0
to 255 (denary) or from 0000 0000 to 1111 1111 (binary).

Note: the term “extended ASCII” will not be tested in an OCR exam, but all ASCII
codes will be represented using 8-bit binary

Activity – ASCII table 1


Search the World Wide Web for an ASCII table.

What are the denary and binary codes for the following characters and controls?
1) 8 2) P (upper) 3) q (lower) 4) * 5) [Carriage Return]

What are the characters or controls represented by the following denary / binary
codes?
6) 27 7) 85 8) 0100 0100 9) 0011 0001 10) 0000 0100
© paullong.net 2020 Page 30 of 54 by Paul Long
The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Sequential codes
Character codes appear in sequence. This means they are in order. Numbers run
from 0 to 9 and letters run from A to Z and a to z. Therefore, it is possible to work
out a character code if a similar character code is already known.

Example – sequential codes


You are given these ASCII character codes:

Character ASCII Code


3 51
F 46
r 114

From this, you can work out that the number 4 will have the next ASCII code after
the number 3 which will be 52 (51 + 1). You can also work out that G will have the
next ASCII code after F which will be 47 (46 + 1).

To work out further codes, draw a small table. For example, to work out the code
for the character y:

Character ASCII Code


r 114 you have been told this above
s 115 add 1
t 116 add another 1
u 117 add another 1
v 118 add another 1
w 119 add another 1
x 120 add another 1
y 121 add another 1

© paullong.net 2020 Page 31 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Activity – ASCII table 2


Here are some ASCII character codes:

Character ASCII Code Denary


5 0000 0101 53
P (upper) 0101 0000 80
c (lower) 0110 0011 99

Without using a full ASCII table, work out the denary and binary codes for the
following characters:

1) 2 2) 9 3) t 4) g 5) R 6) B

Without using a full ASCII table, work out the characters for the following character
codes:

7) 49 8) 0011 1001 9) 73 10) 0101 1000 11) 109 12) 0110 0001

Write out the sentence represented by these ASCII denary codes:


67 111 110 103 114 97 116 117 108 97 116 105 111 110 115 44 32 121 111 117 32 100
105 100 32 105 116 33

Extension: write out the sentence represented by these ASCII binary codes:
01010111 01101111 01110111 00100001 00100000 00100000 01010100 01101000
01100001 01110100 00100000 01110111 01100001 01110011 00100000 01101000
01100001 01110010 01100100 00101110

Extension: set your own challenge using the converter at


http://tiny.cc/BinaryConverter

Unicode
Extended ASCII had many variants and there were still only 256 characters
available. A new character set emerged.

Unicode includes a lot more characters including a wider range of symbols and
characters for many different alphabets.

The first 128 characters (codes 0 to 127) of Unicode are the same as 7-bit ASCII.
After that, Unicode has its own set of codes. For example, character codes 880 to
1023 are Greek characters.

As there are many thousands of Unicode characters, they are usually shown as
hexadecimal codes rather than binary codes.

© paullong.net 2020 Page 32 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Activity - Unicode
Search the World Wide Web for a Unicode table. What are the characters
represented by the following codes?

1) 285 (011C) 2) 952 (03B8) 3) 2835 (0B13)

Relationship to number of bits


The number of bits used in a character set determines the number of characters
that are available. For 7-bit ASCII, there are 128 possible characters (27). For 8-bit
ASCII there are 256 possible characters (28).
more bits = more characters can be represented

Unicode has many variations, but the UTF-32 version uses four bytes for each
character. This gives over 4 million possible characters (232)

The size of a text file is calculated by multiplying the number of bits per character
by the number of characters:
file size (bits) = bits per character x number of characters

file size (bytes) = bits per character x number of characters ÷ 8

Example – text file size


How many bytes are need to store the text file below which uses 8 bits per
character?
Number of characters = 36 (remember
to count spaces and line breaks)
Number of bits = 8 x 36
Number of bytes = 8 x 36 ÷ 8 = 36 bytes

Questions – follow me
1) Define the term character set. [1]

2) Explain why ASCII is needed. [2]

3) a) How many codes are available in 7-bit ASCII? [1]


b) How many codes do you think would be available in 8-bit ASCII? [1]

4) The ASCII denary code for T is 84. What is the ASCII denary code for Q? [1]

5) The Unicode code for g is 0110 0111. What is the Unicode code for h? Show
your working [2]

6) Give two reasons why Unicode might be used instead of ASCII. [2]
© paullong.net 2020 Page 33 of 54 by Paul Long
The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

6) Representing images

Pixels can be used to represent images. A pixel is short for picture element and is a
single point in an image.

Example – pixels in an image


Here is an example of pixels in an image:

This is 18 pixels high and 18 pixels wide.

This low quality image of Pacman ghosts shows how images are made from pixels:

Can you work out approximately how many pixels there are horizontally and
vertically?

Images are displayed as a set of pixels which form together to make an image.
Visual Display Units (VDUs / monitors) display images using pixels. A true high
definition monitor will have 1920 pixels in width and 1080 pixels in height. This works
out at a total of approximately 2 million pixels (2,073,600).
© paullong.net 2020 Page 34 of 54 by Paul Long
The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

The higher the number of pixels for the same image, the better quality an image
will be. This is because there are more pixels which appear smaller and cannot be
seen and so the ‘blocky’ pixelated effect is not noticed by the human eye. More
pixels can also be used to create the image.

Example – higher quality image


This higher definition image of the Pacman ghosts has more pixels and so the
individual pixels are not visible to the human eye and the image looks a higher
quality:

If the number of pixels is reduced, the image will become pixelated.

Activity – Pacman ghosts


Open the file pacman-ghosts.jpg and zoom in on the image. You should start to
see the pixels that make up the image.

Video
Watch the Pixels movie trailer at http://tiny.cc/pixels

Notice how some images are pixelated and the pixels can be clearly seen.

Bitmaps
A bitmap is an image formed from pixels.

Bitmap size
The size in pixels of a bitmap is calculated by multiplying the width of the image in
pixels by the height of the image in pixels:

Bitmap size in pixels = Width x Height

Note: this is often misconceived as the image resolution. Resolution can have
different meanings for images, cameras, screens and printing.

© paullong.net 2020 Page 35 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Example – bitmap size


This bitmap is 8 pixels high by 16 pixels wide:

Bitmap size in pixels = 8 x 16 = 128 pixels

Activity – bitmap size


Calculate the size in pixels of the following bitmaps:

1) 2)

3) A bitmap that is 24 pixels wide and 12 pixels high.


4) A bitmap that is 1024 pixels wide and 768 pixels high.

© paullong.net 2020 Page 36 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Colour depth
The colour depth is the number of bits (binary digits) used to represent each pixel.
A black and white image has 2 colours and therefore only needs a single bit to
represent white as 1 (one) and black as 0 (zero). A black and white image
therefore has a colour depth of 1 (one) because it only uses 1 bit.

Example – 1-bit colour image

This bitmap image could be represented by the following bit pattern:

1100 0111 0011 0100

1 represents each white pixel and 0 represents each black pixel.

Activity – 1-bit colour image


Represent this 1-bit colour bitmap image as a bit pattern.

A colour depth of 2 would use 2 bits. This gives 4 combinations of colours:

00 or 01 or 10 or 11

More bits means a higher colour depth and that means more colours can be used.
These are the number of colours that can be achieved with each colour depth:

Colour Depth Number of Colours Power of 2


1 2 21
2 4 22
4 16 24
8 256 28
16 65,536 216
24 16,777,216 (16.8 million) 224
32 4,294,967,296 (4 billion) 232

more bits = higher colour depth = more colours


reducing the colour depth may reduce the quality of an image
You should notice that the number of colours is one more than the highest denary
number that can be achieved for that length of bit pattern. It follows the binary
line:
colour depth 27 26 25 24 23 22 21 20
number of colours 128 64 32 16 8 4 2 1

d = colour depth number of colours = 2d


© paullong.net 2020 Page 37 of 54 by Paul Long
The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Example – 2-bit colour image


This bitmap image uses 4 colours:

The colours are represented in an index by the following 2-bit patterns:

White = 00
Red = 01
Yellow = 10
Black = 11

The bit pattern for the whole image can be worked out by substituting each colour
with its 2-bit pattern:

The whole image is represented as:

1111 0001 0111 1010 1000 1101 0010 1100

Activity – 2-bit colour image


Represent this 2-bit colour bitmap image as a bit pattern.

Representing black and white bitmaps


You will have seen earlier how for a 1-bit black and white bitmap, each white pixel
could be represented by a 1 (one) and each black pixel is represented by a 0
(zero). When storing the data for a black and white bitmap, the pixels are read
from left to right starting at the bottom.

Example – convert bitmap to binary

Starting with the bottom row, the binary representation would be:
0111 0000 1010 0011

Then the next row up: 1010 1001 0101 1101

Finally, the top row: 1 1 0 1 1 0 0 1 1 0 1 0 0 0 1 1

The whole binary representation for this image is:

0111 0000 1010 0011 1010 1001 0101 1101 1101 1001
1010 0011
© paullong.net 2020 Page 38 of 54 by Paul Long
The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

This is the process the computer follows when storing a black and white bitmap.

Activity – convert bitmap to binary


Convert this black and white bitmap image into binary data:

Convert the other images on this worksheet to binary data.

Watch the video at http://tiny.cc/bwvideo and then use the pixelation widget at
http://tiny.cc/bwpuzzle to change the dimensions and create an image.

Extension activity – representing colour images


Draw a simple bitmap image using 4 colours. Represent the image using binary.
Give your binary code to a friend and ask them to re-create the image. You will
need to give them the binary encoding for each colour.

Repeat the above for an image with 8 colours.

Metadata
Meta data is data about data. For a file, metadata can include:
• type of file (eg bitmap)
• file size (eg 3 Mb)
• date created (eg 20-12-2017)

For images, metadata will include:


• colour depth
• image height and width (in pixels)
• image resolution (in ppi)
• image type (eg JPEG)

Activity – photographs meta data


Find out what additional metadata is stored for photographs.

Use http://tiny.cc/meta-data to find out what meta data is stored about some of
your images.

© paullong.net 2020 Page 39 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Bitmap file sizes

Effect on file size


A bitmap’s file size is affected by two main factors:

• size in pixels (width x height)


• colour depth

Increasing the width, height or colour depth will increase the file size. Files can
therefore be kept smaller by not having lots of spare white pixels and only using
the necessary number of colours for the colour depth.

Example – reducing file size

This image has lots of unnecessary white pixels around the edge. Its size in pixels
can be reduced from 128 to 36 by removing the unnecessary white pixels:

Width = 6
Height = 6
Size in pixels = 6 x 6 = 36

If the image above had a colour depth of 8 then that would allow for 256 colours.
This could be reduced to a colour depth of 1 (one) to keep the file size smaller.

© paullong.net 2020 Page 40 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Calculating a bitmap file size


Each pixel needs to be stored. Each pixel needs a colour. The size of the bit-
pattern for each pixel will determine the overall size of the file.

Note: The size of the file will also include metadata, but at GCSE level you only
need to calculate the storage space needed for the pixel data.

Minimum Bitmap File Size (bits) = Width x Height x Colour Depth

Size (b) = W x H x D

Example – bitmap file size (bits)


A bitmap image is 28 pixels wide and 45 pixels high. It has a colour depth of 8.
Construct an expression to calculate its file size in bits.

Size (b) = W x H x D

Size (b) = 28 x 45 x 8

When data is stored, it is often measured in bytes. You may recall from earlier in
this chapter that there are 8 bits in a byte.

Size in bytes = size in bits ÷ 8

Size (B) = W x H x D ÷ 8

Example – bitmap file size (bytes)


A bitmap image is 32 pixels wide and 15 pixels high. It has a colour depth of 16.
Construct an expression to calculate its file size in bytes.

Size (B) = W x H x D ÷ 8

Size (B) = 32 x 15 x 16 ÷ 8

© paullong.net 2020 Page 41 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Activity – bitmap file size


Calculate the minimum file size in bytes of the following images. Show your
working.

1) A bitmap that is 10 pixels high, 1 pixel wide and has a colour depth of 4.

2) A bitmap with 4 colours, 16 pixels wide and 18 pixels high.

3) A bitmap that is 600 pixels wide, 400 pixels high and has a colour depth of 16.

4) A true high definition bitmap with a colour depth of 24.


a) Calculate the size in bytes.
b) Calculate the size to the nearest megabyte (MB).

5) Calculate the minimum file size in bytes of this 2-bit image:

Note: the method that has been used at GCSE is known as an indexed bitmap
because each pixel is represented by a bitmap pattern that directly
represents a colour. Photographs tend to be represented using values for
RGB (red, green, blue). Watch the video below for more information, but
you do not need to know this for the exam.

© paullong.net 2020 Page 42 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Extension video
Watch http://tiny.cc/rgb-bitmap to find out how photographs are stored using the
RGB method. Notice how 3 channels of one byte each gives a colour depth of 24.

Questions – follow me
1) What is a pixel? [1]

2) Describe what a bitmap image is. [1]

3) A bitmap image is 21 pixels high and 3 pixels wide. How many pixels are in the
image? [1]

4) a) Define colour depth. [1]


b) How many colours can be used with a colour depth of 8? [1]
c) What is the minimum colour depth of an image with 4 colours? [1]

5) The image below is a bitmap.

a) Calculate the minimum file size of this image. Show your working. [4]
b) Explain why the actual file size is slightly more than you have calculated.
[2]
6) Charlie has produced an image. Part of the bitmap image is shown below
with each letter representing a colour:

B B W R Y
B B W R R
B W W R R
Y B B R R
Y Y B W W

Using the example shown above, explain how a bitmap image is stored on a
computer. [5]

© paullong.net 2020 Page 43 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

7) Representing sound

Throughout this section, the following terms will be used:

Computing Term Definition


Amplitude A measurement of sound
Sample Measure of amplitude at a specific time
Bit depth Number of bits used for each sample
Sample rate Number of samples per second

Sampling
Sound begins life in an analogue format. This means it has no discrete (exact)
value and varies continuously. It can be represented in the form of a wave as
shown below:

Equilibrium

As time passes (horizontal x-axis) the sound changes amplitude, which is the
distance from the equilibrium (middle) position.

Video
Watch the sound waves change in this music video. Notice how the distance from
the equilibrium (amplitude) is greater when the sound is louder.

An analogue sound wave can be converted to digital using an analogue to


digital converter. The analogue sound wave will be converted to an analogue
electrical signal and then samples of the analogue signal are taken at regular
intervals. At each interval, the amplitude will be recorded digitally.

sample = measure of amplitude at a specific time

The samples will be taken thousands of times per second. The more samples that
are taken, then the higher quality the digital sound will be because more
measurements of the sound are taken. The number of samples taken per second
is known as the sample rate which is measured in hertz (Hz). Each sample will be
stored using binary.

© paullong.net 2020 Page 44 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

1 Hz = 1 sample per second


500 Hz = 500 samples per second
1 kHz (kilohertz) = 1,000 samples per second
300 kHz (kilohertz) = 300,000 samples per second
1 MHz (megahertz) = 1 million samples per second

Video
Watch http://tiny.cc/samplerate for an overview of sample rate and to hear the
effect of the sample rate on sound.

Example - sampling
11
10
9
8
7
6
5
4
3
Amplitude

2
1
0
-1 0 1 2 3 4 5 6 7 8 9 10
-2
-3
-4
-5
-6
-7
-8
-9
-10
-11
Time (milliseconds)

For this analogue sound, a sample will be taken from the analogue electrical
signal every millisecond. This is a sample rate of 1 kHz (1,000 samples per second).
At 4 milliseconds, the sample taken is an amplitude of 6 and at 7 milliseconds it is -
4.

The samples are recorded as follows:

Time (ms) 1 2 3 4 5 6 7 8 9 10
Amplitude 10 -4 0 6 6.5 0 -4 5 3 -1

© paullong.net 2020 Page 45 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

The digital sound is shown in orange in the graph below. As you can see, some of
the sound is missed out. This is why it is important to have a high enough sample
rate to maintain the original quality.
11
9
7
5
3
Amplitude

1
-1 0 1 2 3 4 5 6 7 8 9 10
-3
-5
-7
-9
-11
Time (milliseconds)

Storing sound files


Computers only use bits (1s and 0s). The amplitude is measured and recorded in
bits. If we only used 2 bits to record the amplitude in the example above, then we
would only be able to record 4 values: 00, 01, 10 and 11 (0, 1, 2 and 3 in denary).
This would mean that sound levels would have to be approximated (e.g. to 0, 3, 6
and 9) and each measurement is a less accurate measure of the original
amplitude. This would reduce the sound quality.

Therefore, an appropriate number of bits needs to be used when recording the


amplitude. This is known as the bit depth. In the same way that the quality of an
image is related to its colour depth, the quality of sound is related to the bit depth.
The higher the bit depth, the higher the quality of the sound.

Video
Watch http://tiny.cc/bitdepth to hear the effect of the bit depth on the dynamic
range of an audio recording.

© paullong.net 2020 Page 46 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Example – reduced bit depth


The digital sound from the example above now has a lower bit depth of 3 bits (8
possible values of -9, -6, -3, 0, 3, 6, 9, 12) in the graph below. Notice how even
more of the original sound quality has been lost.
12

6
Amplitude

0
0 1 2 3 4 5 6 7 8 9 10
-3

-6

-9
Time (milliseconds)

Video
Watch http://tiny.cc/quantizing for a summary of sample rate and how quantizing
is used to give the bit depth.

The minimum size of a sound file depends on 3 factors:


• bit depth (number of bits per sample)
• sample rate (number of samples per second)
• length of the sound

For a single channel of sound, the bit rate of a sound file is calculated as:

bit rate (bits per second) = bit depth x sample rate

The formula for calculating the size of a single channel sound file is:

file size (bits) = sample rate x bit depth x seconds

This could also be calculated as:

file size (bits) = bit rate x seconds

Note: a sound file will also include some metadata which requires a small amount
of storage.
© paullong.net 2020 Page 47 of 54 by Paul Long
The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Example – calculating sound file size


A sound file has a bit depth of 16 bits. The sample rate is 44.1 kHz. The sound lasts
for 4 minutes.

First calculate the time in seconds: 4 x 60 = 240 seconds


Then calculate the sample rate in Hz: 44.1 x 1,000 = 44,100 Hz

File size (bits) = sample rate x bit depth x seconds

File size = 44,100 x 16 x 240 = 169,344,000 bits

You can calculate this in bytes by dividing by 8: 169,344,000 ÷ 8 = 21,168,000 bytes

This is the same as 21.168 MB.

Video
Watch Digital Audio Explained at http://tiny.cc/audioexplained for an explanation
of the terms used during this section.

Activity – calculating sound file size


Calculate the size in bits of the following sound files:

1) A 20 second telephone call recorded with a sample rate of 8,000 Hz and a bit
depth of 8.

2) A 45 second music file that has a sample rate of 22 kHz and a bit depth of 16.

3) A 3 minute CD track that has a sample rate of 44.1 kHz and a bit depth of 16.

4) A 4.5 minute music file that has a sample rate of 11,025 Hz and a bit depth of
4.

5) A 3 minute 40 second voice recording that has a sample rate of 16,000 Hz and
a bit depth of 8.

Extension: calculate each of the above in bytes and then calculate in kB or MB.

© paullong.net 2020 Page 48 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Note: a sound file will often include more than one channel of sound; to
calculate the file size of a sound file with more than one channel, simply
multiply your answers by the number of channels:

file size (bits) = sample rate x bit depth x channels x seconds

Activity – sound quality


Use sound editing software to change the sample rate and bit depth of different
types of sounds and listen to the effect. If you don’t already have any sound
editing software, you could download Audacity from http://tiny.cc/audacity

Questions – follow me
1) Explain why sound needs to be converted to a digital format. [2]

2) a) Define the term sample. [1]


b) Define sample rate. [1]
c) Define bit depth. [1]

3) State 3 factors that affect the size of a sound file. [3]

4) a) Which unit is bit depth measured in?


A) Hertz (Hz)
B) Milliseconds (ms)
C) Bits per second (bps)
D) Bits [1]

b) Which unit is bit rate measured in?


A) Hertz (Hz)
B) Milliseconds (ms)
C) Bits per second (bps)
D) Bits [1]

c) Which unit is sample rate measured in?


A) Hertz (Hz)
B) Milliseconds (ms)
C) Bits per second (bps)
D) Bits [1]

© paullong.net 2020 Page 49 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

5) a) Which one of the following is true if you increase the bit depth?
A) File size reduces
B) Sound quality reduces
C) Sound quality increases
D) Sample rate increases [1]

b) Which one of the following is true if you increase the sample rate?
A) File size reduces
B) Sound quality reduces
C) Bit depth increases
D) Bit rate increases [1]

6) a) Explain why a high quality sound file needs a high sample rate. [2]
b) Explain why a high quality sound file needs a high bit depth. [2]

7) John records himself singing a song.


a) Outline what happens when the computer converts his voice into a file. [3]

John reduces the bit depth used to store his recorded song.
b) Explain two effects this will have on his recorded song. [4]

© paullong.net 2020 Page 50 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

8) Data compression

Video
Watch this explanation of file compression http://tiny.cc/compressionbbc from
BBC Bitesize.

The need for compression

File sizes can get quite large, particularly if they contain images, sound or video.
When storage space is limited, it is useful to be able to reduce the size of files so
that more can be fitted on to the storage medium. Compression is reducing the
number of bits needed to store data.

1 2 3 4 5 6 7

1 2 3 4 5 6 7 Space for more files

Imagine you have a USB flash memory stick and you want to store some videos of
you and your friends. If each video uses up 4 GB of storage and you have 10
videos then you will need 40 GB of space. If your memory stick is only 16 GB in size
then you will only be able to store 4 of the videos. However, if the videos have
been compressed to 1.5 GB each then you can store all 10 of them on a 16 GB
USB memory stick.

It’s also good to compress files for use on the Internet. When transferring large files
it takes a long time, but if they are compressed in size then they can be transferred
more quickly. Imagine you are trying to view the BBC website on a slow 3G mobile
connection. It can take ages to open the page. Now imagine what it would be
like if the images on the website weren’t already compressed – it would take even
longer.

You may also have experienced occasions when you have sent an email with a
large attachment but it cannot be received at the other end. This is because the
recipient has a file size restriction on their inbox. Compressing the file might make it
small enough to be allowed to be received.

Types of compression
There are two categories of compression. Lossy and lossless. Lossy compression
means that parts of a data file are lost when the file is made smaller. This could be
by reducing the number of pixels in an image, or reducing the bit depth of a
sound file or reducing the sample rate. Once those bits of data have been
removed, they cannot be retrieved.

© paullong.net 2020 Page 51 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Example – lossy compression


This is a photograph of a reptile.

To compress the image, the number of pixels can be reduced:

The image is no longer as high quality as much of the data has been lost.

The trick with lossy compression is to reduce the file size by removing aspects that a
human cannot see or hear and so make no difference to a person.

© paullong.net 2020 Page 52 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net
Examples of lossy compressed files include:
• JPEG (images)
• MP3, MP4 and OGG (sound)
• WMV, MPEG (video)

Lossless compression reduces a file’s size without losing any data. There are many
methods that can be used including grouping data together.

Example – lossless compression


This uncompressed image of Mario has
208 pixels and a colour depth of 3
which would require 78 bytes of
storage.

However, there are lots of parts of this


image that could be grouped
together. The bottom row could be
expressed as 4 x brown, 4 x white, 4 x
brown.

The 7th row up could be set to 4 x red, 4


x blue, 4 x red. This requires a lot less
storage space than storing the colour
of every single pixel.

Examples of lossless compressed files include:


• PNG (images)
• FLAC, ALAC (sound)

If lossless compression is an option, you might be wondering why lossy compression


is used. There are two reasons for using lossy compression. The first is that sound
can rarely be compressed using lossless compression. The second is that lossy
compression can often compress a file to a smaller file size than lossless
compression can.

Video
Watch this http://tiny.cc/conveyor (supermarket conveyor belt) analogy of one
method of compression known as run-length coding.

Activity - compression
Watch http://tiny.cc/compressvideo from code.org and then use the text
compression widget from http://tiny.cc/compresspuzzle

© paullong.net 2020 Page 53 of 54 by Paul Long


The Ultimate GCSE CS textbook for OCR – Chapter 2 Published by paullong.net

Questions – follow me
1) Contrast lossy and lossless compression. [2]

2) Give 2 reasons for compressing data. [2]

3) John has recorded himself singing. He needs to send the recording and the
lyrics (words of the song) by email to his music teacher.

a) Explain why lossy compression is suitable for the sound recording. [2]
b) Explain why lossy compression is not suitable for the lyrics. [2]

© paullong.net 2020 Page 54 of 54 by Paul Long

You might also like