Essential Maths Skills For ASA Level Computer Science Nodrm
Essential Maths Skills For ASA Level Computer Science Nodrm
for AS/A-level
Computer
Science
Gavin Craddock and Victoria Ellis
Philip Allan, an imprint of Hodder Education, an Hachette UK company, Blenheim Court, George Street,
Banbury, Oxfordshire OX16 5BH
Orders
Bookpoint Ltd, 130 Park Drive, Milton Park, Abingdon, Oxfordshire OX14 4SE
tel: 01235 827827
fax: 01235 400401
e-mail: [email protected]
Lines are open 9.00 a.m.–5.00 p.m., Monday to Saturday, with a 24-hour message answering service. You
can also order through the Hodder Education website: www.hoddereducation.co.uk
© Gavin Craddock and Victoria Ellis 2016
ISBN 978-1-4718-6357-8
First printed 2016
Impression number 5 4 3 2 1
Year 2020 2019 2018 2017 2016
All rights reserved; no part of this publication may be reproduced, stored in a retrieval system, or
transmitted, in any other form or by any means, electronic, mechanical, photocopying, recording or
otherwise without either the prior written permission of Hodder Education or a licence permitting restricted
copying in the United Kingdom issued by the Copyright Licensing Agency Ltd, Saffron House, 6–10 Kirby
Street, London EC1N 8TS.
Typeset in India
Cover illustration: Barking Dog Art
Printed in Spain
Hachette UK’s policy is to use papers that are natural, renewable and recyclable products and made from
wood grown in sustainable forests. The logging and manufacturing processes are expected to conform to
the environmental regulations of the country of origin.
Contents
The content listed in bold is only specified to be assessed by AQA and is not assessed by
OCR, WJEC or Eduqas.
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2 Number bases
Converting a base 2 or base 16 number to base 10 . . . . . . . . . . . . . . . . . . . . . .14
Converting a base 10 to a base 2 or base 16 number . . . . . . . . . . . . . . . . . . .16
3 Units
Binary prefixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Decimal prefixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
5 Binary numbers
Signed vs unsigned binary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Key values for n bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Binary addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Binary multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Representing negative numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Fixed point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Logical shifts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Bitwise operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
6 Floating point data representation
Floating point binary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Normalisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
8 Boolean algebra
Logic gates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Boolean algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76
9 Vectors
Vector representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Vector addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Scalar vector multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Dot product of two vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Convex combination of two vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Exam-style questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Appendix
Specification cross–reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
The new A-levels in computer science have incorporated mathematical skills that form
the fundamentals of computing. Computers cannot function without mathematics. Even
the basics, such as the storage of data, rely on the use of binary numbers. Creating images
relies on vectors, and writing algorithms involves mathematical functions.
This book is intended to help anyone who is struggling with the mathematics skills
involved, or who would like to consolidate or revise the key skills. It is not specific to
one examination board, but instead covers all of the different topics that each syllabus
requires. Take a look at the cross-reference chart (page 103) to check what is needed for
the specification you are studying. Likewise, particular topics that are relevant to only
one examination board are also indicated in the table of contents.
Each skill is broken down into a series of easy-to-follow steps, with worked examples
to walk you through the application of these skills. There is then a series of guided
questions, where some or all of the instructions are given, asking you to complete the
actions and fill in any gaps to reach the solution. Finally, there are practice questions for
you to work through on your own.
We hope that this book will give you more confidence with the mathematical concepts of
computer science and support you in your studies. Good luck!
Full worked solutions to the guided and practice questions and exam-style questions can
be found online at www.hoddereducation.co.uk/essentialmathsanswers.
Introduction 5
This page intentionally left blank
1 Number systems and sets
Number systems
Natural numbers
Natural numbers are positive whole numbers, including 0. The symbol ℕ is used for the
set of natural numbers, so that ℕ = {0, 1, 2, 3, …}
The natural numbers are used for counting (such as ‘there are 17 students in this
class’) and ordering (such as ‘the team finished in 3rd place’). In computer science, it
is important to remember that, if a member of ℕ is added to or multiplied by another
member of ℕ, the result is also a member of ℕ. This allows natural numbers to be used as
counters in computer programs.
Integers
Integers are negative and positive whole numbers, again including 0. The symbol ℤ is
used for the set of integers, so that ℤ = {…, –3, –2, –1, 0, 1, 2, 3, …}. ℤ is used for integers
because ‘Zahlen’ is German for ‘numbers’.
Just as with natural numbers, if a member of ℤ is added to or multiplied by another
member of ℤ, the result is also a member of ℤ. This is also true for subtraction as ℤ
includes the negative whole numbers. Integers are therefore very useful as counters in
computer programs for whole quantities that may be negative.
Rational numbers
Rational numbers are those numbers that can be written as a fraction, such as 7 or 3 .
5 2
The symbol ℚ is used for the set of rational numbers and can be thought of as standing
for ‘quotient’ to aid remembering.
x
A rational number can be defined as a number in the form , where x and y are both members
y
9
of ℤ and y is not zero. Since a number such as 9 can be written as the fraction , ℚ includes
1
all members of ℤ. When any rational number is written as a decimal, it either terminates after
1 1
a finite number of digits (such as = 0.125) or recurs (such as = 0.333333333…).
8 3
Irrational numbers
Irrational numbers are those numbers which are not rational: that is, they cannot be
written as a fraction. Famous examples of irrational numbers include π, e and 2 .
π can be written as 3.141592… but this series of digits will continue infinitely. The world
record for the number of digits calculated for π currently stands (as of 2015) at 2.7 trillion
digits. For this reason, any computer representation of an irrational number will actually
be a very close approximation.
Real numbers
Real numbers are the set of all possible real world quantities. Another way of thinking
about real numbers is that they are the points on an infinitely long line. Every possible
value, both positive and negative, appears somewhere on the line. The symbol ℝ is used
for this set. ℝ contains all natural numbers (ℕ), integers (ℤ), rational numbers (ℚ) and
irrational numbers.
Real numbers are very useful for measuring. For example, if three people ran a race, their
times (in seconds) may be 15, 17.3 and 18.3454. These are all real numbers.
A Worked examples
a For each of the following, explain which set(s) of numbers they belong to (either natural,
integer, rational, irrational or real). A number may be a member of more than one set.
i 35
■ Because this is a whole number (with no decimal places), this is a natural number and also
an integer.
35
■ Because it can be written in the form , it is also a rational number.
1
■ Finally, it is also a real number.
ii 12.5
■ The decimal point means that this is definitely not a natural number or an integer.
■ It can be written in the form 25 , so is therefore a rational number.
2
■ In addition, it is also a real number.
iii 3
■ In decimal form, this is 1.4142135… It does not terminate or recur, therefore it is an irrational
number.
■ Just like every other number, it is also a real number.
b Decide whether each of the following statements is true or false.
i All natural numbers are also integers.
This is TRUE. The set of integers includes all natural numbers AND also includes negative
numbers.
ii A square root is always irrational.
This is FALSE. Most roots (such as 2 or 3 ) are irrational. However, 4 = 2 and 9 = 3.
These are certainly not irrational!
iii A number is either rational or irrational, but never both.
This is TRUE. The definitions of these number sets are opposites: if a number is rational then it
cannot be irrational and vice versa.
C Practice questions
3 For each of the following, state which number set(s) it belongs to (either natural,
integer, rational, irrational or real). A number may be a member of more than one set.
a (4 + 4)
b 4 + 4
c 17 – (10 + 9)
16
d
3
Sets
Note: this topic is assessed at A-level by AQA only.
Table 1.1
Symbol Explanation
… An ellipsis is used to show a regular sequence. {1, 2, 3, …, 100} is the set of all
natural numbers between 1 and 100. The ellipsis shows that all numbers in between
3 and 100 are also included.
| ‘Such that’. This is used as a separator with the variable to the left and the rule to
the right.
∈ ‘is a member of’. x ∈ ℕ means that x is a member of the set of natural numbers.
∧ ‘AND’. x > 7 ∧ x < 10 means x is greater than 7 and x is less than 10.
Subsets
A subset is defined as a set that is wholly contained within another set. For example,
if A = {1, 2, 3, 4} and B = {2, 3} then B is a subset of A. The symbol ⊆ is used
to mean subset, so B ⊆ A reads ‘B is a subset of A’. The line in this symbol is
reminiscent of an equals sign because a subset can technically have exactly the same
elements as the superset. That is, if D = {1, 2, 3, 4} and E = {1, 2, 3, 4}, D = E and
D ⊆ E are both true.
A proper subset uses the symbol ⊂ and is defined as a subset where at least one element
is in the superset that is not in the subset. For example, {1, 2, 3} is a proper subset of ℕ
because there is at least one element in ℕ that is not in {1, 2, 3}.
Set operations
The main operations that can be carried out on sets are union, intersection, difference
and Cartesian product.
■ The union of sets A and B is the set of all elements that are a member of either or
both sets. The symbol ∪ is used to show union. If A = {1, 3, 7, 9} and B = {1, 2, 4, 7}
then A ∪ B = {1, 2, 3, 4, 7, 9}.
■ The intersection of sets A and B is the set of all elements that are members of both
sets. The symbol ∩ is used to show intersection. Using the same sets for A and B
above then A ∩ B = {1, 7}.
■ The difference of sets A and B is the set of all elements that are members of A but not
members of B. The symbol \ is used to show difference. Again using the same sets as
above, A \ B = {3, 9}.
■ The Cartesian product of two sets is the set of all possible ordered pairs between the
two sets. The symbol × (the same symbol that is used for multiplication) is used to
denote a cartesian product. For example, if A = {1, 7} and B = {True, False} then
A × B = {(1, True), (1, False), (7, True), (7, False)}.
ii A∩B
The intersection of two sets includes the elements that are members of both sets. A ∩ B = {7, 8}
iii A \ B
The difference of two sets is simply the elements that are in the first which are not in the second.
A \ B = {9, 10}
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
1 Find A where A = {x | x ∈ ℕ ∧ x > 4 ∧ x < 10}.
Step 1: understand the set comprehension statement. This is asking for all of the
values of x, such that x is a member of the set of natural numbers AND x is larger
than 4 AND x is smaller than 10.
Step 2: write the distinct values that meet all of these criteria between {curly braces}.
2 Find A where A = {3x – 1 | x ∈ ℕ ∧ x > 5}.
Step 1: find all values of x that are members of the set of natural numbers AND larger
than 5.
Step 2: when you have these values, use the equation 3x – 1 to find the elements of
the set A.
Step 3: write the values that you obtained in Step 2 between {curly braces}. If the
values continue in a regular sequence, use the ellipsis (…) to show this.
3 If A = {7, 9, 12} and B = {9, 12, 15}, find A ∪ B.
Step 1: decide the appropriate operation for the symbol ∪.
Step 2: compare both sets using the operation from Step 1 and list all the elements.
C Practice question
5 If A = {1, 2, 3, 4, 5, 6}, B = {2, 4, 6, 8} and C = {6, 7, 8, 9}, find:
a B∩C
b C\B
c (A ∩ B ) ∪ C
d (C ∩ B) \ A
e {x | x ∈ B ∧ x >3}
g {2x | x ∈ C }
f {x2 | x ∈ (A ∪ C) ∧ x < 8}
‘Number base’ means the number of different symbols that are used. So if you have base
10 (known as decimal, or denary), there are 10 different symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
These symbols are then combined to represent any number, e.g. 12, 99, 386.
Binary is base 2 and there are two different symbols: 0 and 1.
Hexadecimal is base 16 and there are 16 different symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B,
C, D, E, F. Letters are used in hexadecimal instead of two-digit numbers.
A = 10 C = 12 E = 14
B = 11 D = 13 F = 15
Converting a base 2 or
base 16 number to base 10
Each digit in a number can be converted into its decimal (base 10) value using four
steps as shown in the Worked examples that follow. Table 2.1 shows how the digits are
presented in table format. In computer science we start counting at 0. The first digit (on
the right) is Digit 0 (D0). This is multiplied by its base to the power of 0 (base0). Digit 1
(D1) is multiplied by its base to the power of 1 (base1). Digit 2 (D2) is multiplied by its
base to the power of 2 (base2) and so on.
Table 2.1
D4 D3 D2 D1 D0
× base 4
× base 3
× base 2
× base 1
× base0
2 Number bases
A
a Convert the binary number 0101 to decimal.
Step 1: put the binary number into the table format as shown in Table 2.1.
0 1 0 1
× base3 × base2 × base1 × base0
0 1 0 1
× 23 × 22 × 21 × 20
0 × 23 = 0 1 × 22 = 4 0 × 21 = 0 1 × 20 = 1
1 D 9
× base2 × base1 × base0
Step 2: hexadecimal is base 16, so replace the word ‘base’ with 16.
1 D 9
× 162 × 161 × 160
Step 3: calculate each column, replacing any letters with their numerical equivalent. (D = 13; see box
on previous page.)
2 Number bases 15
B Guided questions
2 Number bases
Copy out the workings and complete the answers on a separate piece of paper.
1 Convert the binary number 11100 to denary. Show your working.
Steps 1 and 2: put the number into table format and fill in the number base for binary
in each column. One has been done for you.
1 1 1 0 0
× 24 × 3
× 2
× 1
× 0
Steps 3 and 4: complete the calculation for each column and add the columns. Again,
one has been done for you.
16 + + + +
The answer is .
2 Convert the hexadecimal number 3F to denary. Show your working.
Steps 1 and 2: put the number in table format and fill in the second hexadecimal
number on the top row and the bases on the bottom row.
3
1 0
× ×
Steps 3 and 4: complete the calculation for each column and add the columns.
+
The answer is .
C Practice questions
3 Convert the binary number 1011 to decimal.
4 Convert the hexadecimal number 29 to decimal.
5 Convert the binary number 10111 to decimal.
6 Convert the hexadecimal number AB to decimal.
7 Convert the binary number 10110110 to decimal.
8 Convert the hexadecimal number 32F1 to decimal.
Converting a base 10 to a
base 2 or base 16 number
Using division, you can convert a base 10 (decimal) to both base 2 (binary) and base
16 (hexadecimal).
2 Number bases
a Convert 2910 to base 2 (binary).
■ Divide the number by the base you want it in until you get 0.
■ You want base 2 so you divide by 2.
Step 1: divide the number by 2.
29 ÷ 2 = 14 r 1
Step 2: take the quotient (the result) and divide it by 2.
14 ÷ 2 = 7 r 0
Step 3: repeat this process until you have 0 — quotient divided by 2.
7÷2=3r1
Still not 0? Repeat — quotient divided by 2.
3÷2=1r1
Still not 0? Repeat — quotient divided by 2.
1÷2=0r1
Make sure you write your remainder, as this will be used to make the answer.
Step 4: now you have 0, write all the remainders from the last to the first; 11101.
b Convert 10010 to binary (base 2).
Step 1: divide 100 by 2.
100 ÷ 2 = 50 r 0
Step 2: divide the quotient by 2.
50 ÷ 2 = 25 r 0
Step 3: repeat until you have 0.
25 ÷ 2 = 12 r 1
12 ÷ 2 = 6 r 0
6÷2=3r0
3÷2=1r1
1÷2=0r1
Continue dividing by 2 until you have 0.
Step 4: write all the remainders, from the last to the first, 1100100.
c Convert 2910 to base 16 (hexadecimal).
Step 1: divide the number by 16.
29 ÷ 16 = 1 r 13
Step 2: divide the quotient by 16.
1 ÷ 16 = 0 r 1
2 Number bases 17
Step 3: repeat until you have 0.
2 Number bases
1 13 becomes 1D
d Convert 10010 to hexadecimal (base 16).
Step 1: divide the number by 16.
100 ÷ 16 = 6 r 4
Step 2: divide the quotient by 16.
6 ÷ 16 = 0 r 6
Step 3: repeat until you have 0.
Step 4: write the remainders, from last to first, 64.
Step 5: check for numbers over 9. There are none.
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
1 Convert 6110 to binary (base 2).
Step 1: divide the number by 2. 61 ÷ 2 = 30 r 1
Step 2: divide the quotient by 2. 30 ÷ 2 = 15 r
Step 3: repeat until you have 0. 15 ÷ 2 = r
÷2= r
÷2= r
÷2= r
Step 4: write the remainders from last to first.
2 Convert 15610 to binary.
Step 1: divide the number by 2. 156 ÷ 2 = r
Step 2: divide the quotient by 2. ÷2= r
Step 3: repeat until you have 0. ÷2= r
÷2= r
÷ = r
÷ = r
÷ = r
÷ = r
Step 4: write the remainders from last to first.
2 Number bases
Step 1: divide the number by 16. 57 ÷ 16 = 3 r
Step 2: divide the quotient by 16. 3 ÷ 16 = r
Step 3: repeat until you have 0.
Step 4: write the remainders from last to
A = 10 C = 12 E = 14
first, replacing any 2-digit numbers with
their letter. B = 11 D = 13 F = 15
C Practice questions
5 Convert 2510 to binary.
6 Convert 16110 to binary.
7 Convert 24510 to binary.
8 Convert 3810 to hexadecimal.
9 Convert 11710 to hexadecimal.
10 Convert 23310 to hexadecimal.
2 Number bases 19
3 Units
Binary prefixes
After a byte you start recording the number of bits in powers of 10.
Table 3.1
220 Mi Mebi
230 Gi Gibi
240 Ti Tebi
These are called prefixes because they will all come before what they represent, which is
the number of bytes:
■ Kibibyte = 210 bytes = 1024 bytes
■ Mebibyte = 220 bytes = 1 048 576 bytes
■ Gibibyte = 230 bytes = 1 073 741 824 bytes
■ Tebibyte = 240 bytes = 1 099 511 627 766 bytes
Decimal prefixes
These work in the same way. However, for a decimal number, these increase by a power
of 3 each time.
Table 3.2
The decimal prefixes are the ones most commonly known and used in computing,
e.g. a kilobyte (kB) is 1000 bytes, whereas a kibibyte (KiB) is 1024 bytes.
3 Units
You may be asked, for example, how many Mi are in a Ti. You don’t need to learn all
of the different numbers, just remember the patterns and the rule for dividing
40
numbers with powers: Ti = 2 = 240–20
Mi 220
A Worked examples
a How many MiB are in a TiB?
Step 1: write down the values for each.
MiB = 220 TiB = 240
Step 2: work out the difference in powers. Subtract the smaller power (20) from the larger power (40)
to get the new power. 40 – 20 = 20
The answer is 2 to the power of the difference in powers, 20. 220 = 1 048 576
b How many MB are in 2 GB?
Step 1: write down the values for each.
MB = 106 GB = 109
Step 2: work out the difference in powers.
9 – 6 = 3, so 23 = 1000
Step 3: you need 2 GB, so multiply by 2.
1000 × 2 = 2000
You can give the answer in terms of 103, so 2 × 103 would also be acceptable for the answer.
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
1 How many kB are in a GB?
Step 1: write down the values for each.
kB = 103 GB = 109
tep2:workoutthedifferenceinpowers.Writethevalueforthelargerpower−the
S
smaller power.
2 How many MiB are in 4 TiB?
Step 1: write down the values for each.
Step 2: work out the difference in powers.
Step 3: there are 4 TiB, so multiply by 4.
C Practice questions
3 How many kB are in 8 MB? 6 How many GiB are in 5 TiB?
4 How many KiB are in 10 TiB? 7 How many MB are in 12 GB?
5 How many GB are in 3 TB?
3 Units 21
4 Arithmetic operations in a
programming language
When you write in a programming language, it is likely that you will need to perform
calculations. Programming languages have symbols that may vary slightly between
languages, but perform the same functions.
Table 4.1
Symbol Name Function Example
x+y Addition Add two numbers together 2+3=5
x–y Subtraction Take one number away from another 5–2=3
x*y Multiplication Multiply x by y 2*4=8
x/y Division Divide x by y 10/2 = 5
13/2 = 6.5
x MOD y Modulus Find the remainder of the division xy 13 MOD 2 = 1
100 MOD 12 = 4
x DIV y Integer division / Find the integer (whole number) part of the 13 DIV 2 = 6
quotient division xy 100 DIV 12 = 8
This Unit will not look at the basic operations of +, –, * or /, but the operations MOD,
DIV and ^.
DIV
DIV gives the whole, integer, value of the division. In other words, the number before the
decimal point.
A Worked examples
a What is 10 DIV 2?
Step 1: divide the numbers.
10
=5
2
Step 2: the answer is the number before the decimal point, 5.
b What is 15 DIV 9?
15
Step 1: = 1.666
9
Step 2: answer = 1
C Practice questions
3 What is 38 DIV 7?
4 What is 49 DIV 2?
5 What is 100 DIV 3?
6 What is 166 DIV 12?
7 What is 215 DIV 156?
Mod
The modulus works out the number left over after the quotient division, for example,
13 DIV 2 = 6 with 1 left over (2 × 6 = 12), so 13 MOD 2 = 1. Another way of working it
out is using the quotient division, for example:
Step 1: work out the division.
13
= 6.5
2
Step 2: multiply the quotient (6), by the number you divided by (2).
6 × 2 = 12
Step 3: subtract this (12) from the first number (13), giving 1.
A Worked examples
a What is 27 MOD 6?
In computing, the quotient
Step 1: work out the division.
is the part before the
27
= 4.5 decimal point. In 4.5 it is 4.
6
Step 2: multiply the quotient (4) by the number you divided by (6).
4 × 6 = 24
Step 3: subtract this from the first number to get the answer.
27 – 24 = 3
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
1 What is 50 MOD 8?
Step 1: work out the division.
50
= 6.25
8
Step 2: multiply the quotient (6) by the number you divided by (8).
Step 3: subtract this from the first number (50) to get the answer.
2 What is 1 MOD 4?
Step 1: work out the division.
1
= 0.25
4
Step 2: multiply the quotient (0) by the number you divided by (4).
Step 3: subtract this from the first number (1) to get the answer.
C Practice questions
3 What is 12 MOD 3?
4 What is 51 MOD 9?
5 What is 4 MOD 9?
6 What is 20 MOD 10?
7 What is 102 MOD 6?
Power
The ^ is the symbol for ‘to the power of’. Instead of writing 210, in programming you
would enter 2^10.
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
1 What is 5^3?
Multiply the first number (5) by itself by writing it down the second number of times (3).
2 What is 8^4?
Multiply the first number (8) by itself by writing it down the second number of times (4).
C Practice questions
3 What is 3^5?
4 What is 10^3?
5 What is 12^2?
6 What is 4^7?
7 What is 5^5?
8 What is 20^3?
next number is
2 roundDown(number, no. of decimal places) — this always rounds down, no matter
what the next number is
3 round(number, no. of decimal places) — this will round down if the next decimal
place is 0–4, or up if it’s 5–9).
Truncating is slightly different. This cuts off the number at a set place. In computer
science, this function cuts off any numbers after the decimal point. For example, 12.659
truncated would be 12. The function, again, depends on your language. For example,
trunc(number) will remove the decimal places in the number.
A Worked examples
a State the result of the code roundDown(2.956,1).
■ The ‘1’ inside the brackets is the number of decimal places (d.p.).
■ So this function wants the number 2.956 rounded down to 1 d.p.
■ The answer is 2.9.
■ Even though the second d.p. is 5, the roundDown function always takes the number down.
b State the result of the code roundUp(6.95,0).
■ The ‘0’ means there are no d.p. required.
■ The function is to round up, therefore the answer is 7.
c State the result of the code round(2.65,1).
■ The 1 means that 1 d.p. is required, and the code does not state up or down, so it depends on the
next number.
■ The next number is a 5, so it needs to round up.
■ The answer is 2.7 (the 0.05 rounds the 6 up to 7, so 2.65 becomes 2.7).
d State the result of the code trunc(659.451).
trunc removes the decimal places, so the answer is 659.
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
1 State the result of the code roundDown(10.5946,3).
Step 1: how many decimal places does the function want? 3 places
Step 2: does the function want the number rounded up, down, or is it dependent on the
next number?
Step 3: what is the answer?
C Practice questions
5 State the result of the code roundDown(65.21,0).
6 State the result of the code roundUp(99.987,2).
7 State the result of the code round(90.541263,3).
8 State the result of the code trunc(100.111).
9 State the result of the code roundDown(456.123,2).
10 State the result of the code roundUp(20.695,0).
11 State the result of the code trunc(65.65).
12 State the result of the code round(999.999,2).
Logical operations
There are a number of logical operations that you may come across in programming.
These are shown in Table 4.2. Some of the symbols may differ slightly depending on the
programming language you are using, e.g. not equal to may be != or <>. Some symbols
may differ from how they are shown in mathematics.
A Worked examples
a What is the result of 25 <= 60?
Step 1: replace the symbol with its meaning.
25 is less than or greater than 60.
Step 2: is this true?
Yes, so the result is true.
b What is the result of 50 > 50?
Step 1: replace the symbol with the meaning.
50 is greater than 50.
Step 2: is this true?
No, so the result is false.
c What is the result of 100 != 101?
Step 1: replace the symbol with the meaning.
100 is not equal to 101.
Step 2: is this true?
Yes, so the result is true.
C Practice questions
4 What is the result of 20 <= 21?
5 What is the result of 100 > 78?
6 What is the result of 0 != 0?
7 What is the result of 654 < 987?
8 What is the result of 2839 <= 9283?
9 What is the result of 3 >= 3?
10 What is the result of 222 = 111?
11 What is the result of 401 <= 432?
Table 5.1
0 0 0+0=0
×21 ×20
If you have a 2-bit number, the maximum value you can store is 3:
Table 5.2
1 1 2+1=3
×21 ×20
5 Binary numbers
number and the number of different values.
Step 1: the smallest value is 0.
Step 2: the largest value is (2n − 1) (where n is the number of bits).
Step 3: the number of different values is n (where n is the number of bits).
Let’s try it with the example on page 30.
Step 1: the smallest value is 0 (as proven on page 30).
Step 2: the largest value is 2n − 1 where n is the number of bits, which is 2, so replace n with 2.
2n − 1 = 22 − 1 = 4 − 1 = 3.
Step 3: the number of different values is 2n, 22 = 4.
A Worked examples
a Give the smallest value, largest value and the number of different values that can be stored
with 4 bits.
Step 1: the smallest value = 0
Step 2: the largest value = 2n − 1. There are 4 bits. 24 − 1 = 16 − 1 = 15
Step 3: the number of values = 2n = 24 = 16
The smallest number is 0, the largest number is 15, the number of different values is 16.
b Give the smallest value, largest value and the number of different values that can be stored
with 8 bits.
Step 1: the smallest value = 0
Step 2: the largest value = 2n – 1 = 28 – 1 = 256 – 1 = 255
Step 3: the number of values = 2n = 28 = 256
c Give the smallest value, largest value and the number of different values that can be stored
with 1 bit.
Step 1: the smallest value = 0
Step 2: the largest value = 2n – 1 = 21 – 1 = 2 – 1 = 1
Step 3: the number of values = 2n = 21 = 2
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
1 Give the smallest value, largest value and the number of different values that can
be stored with 6 bits.
Step 1: write down the smallest value.
Step 2: write the largest value = 2n – 1 = 26 – 1
Step 3: the number of values = 2n = ?
5 Binary numbers 31
2 Give the smallest value, largest value and the number of different values that can
5 Binary numbers
C Practice questions
3 Give the smallest value, largest value and the number of different values that can be
stored with 7 bits.
4 Give the smallest value, largest value and the number of different values that can be
stored with 10 bits.
5 Give the smallest value, largest value and the number of different values that can be
stored with 11 bits.
6 Give the smallest value, largest value and the number of different values that can be
stored with 16 bits.
Binary addition
This section will show you how to add together two unsigned binary integers. Remember
that unsigned means the binary integer is positive and the most significant bit (leftmost
bit) counts as a value.
Rule 1: 0 + 0 = 0
0 The binary for 0
0 Added to the binary for 0
0 Equals the binary for 0
Rule 2: 0 + 1 = 1 or 1 + 0 = 1
0 The binary for 0
1 Added to the binary for 1
1 Equals the binary for 1
Rule 3: 1 + 1 = 0 carry 1
1 The binary for 1
1 Added to the binary for 1
1 0 Equals the binary for 2 (which is 10)
Rule 4: 1 + 1 + 1 = 1 carry 1
1 The binary for 1
1 Added to the binary for 1
1 Added to the binary for 1
1 1 Equals the binary for 3 (which is 11)
5 Binary numbers
together. Put the numbers above each other, with the binary numbers aligned to the right,
then look at each column from the right, one at a time. If there are 8 bits, look at the
column with the 8th bit in and find which rule applies to it. Then move to the 7th. Carried
digits are put in the column to the left, and they count when applying the rules. Let’s see
it in practice.
A Worked examples
a Add the binary numbers 01111011 and 01101000.
Step 1: put the numbers together (these are in a table to help to get you started).
0 1 1 1 1 0 1 1
0 1 1 0 1 0 0 0
0 1 1 1 1 0 1 1
0 1 1 0 1 0 0 0
1
0 1 1 1 1 0 1 1
0 1 1 0 1 0 0 0
1 1
0 1 1 1 1 0 1 1
0 1 1 0 1 0 0 0
0 1 1
0 1 1 1 1 0 1 1
0 1 1 0 1 0 0 0
0 0 1 1
1
5 Binary numbers 33
Step 6: next column (there is now a bit in the carry that needs to be taken into account). 1 + 0 + 1.
5 Binary numbers
Ignore the 0, there are two 1s, so this follows Rule 3. The answer is 0 carry 1.
0 1 1 1 1 0 1 1
0 1 1 0 1 0 0 0
0 0 0 1 1
1 1
0 1 1 1 1 0 1 1
0 1 1 0 1 0 0 0
1 0 0 0 1 1
1 1 1
0 1 1 1 1 0 1 1
0 1 1 0 1 0 0 0
1 1 0 0 0 1 1
1 1 1 1
0 1 1 1 1 0 1 1
0 1 1 0 1 0 0 0
1 1 1 0 0 0 1 1
1 1 1 1
Once you have completed an addition, convert the binary numbers to check you have done it
correctly.
0 1 1 1 1 0 1 1 = 123
0 1 1 0 1 0 0 0 = 104
1 1 1 0 0 0 1 1 = 227
1 0 1 1 0 1 1 1
1 1 0 0 0 1 1 1
0
1
1 0 1 1 0 1 1 1
1 1 0 0 0 1 1 1
1 0
1 1
1 0 1 1 0 1 1 1
1 1 0 0 0 1 1 1
0 1 1 1 1 1 1 0
1 1 1 1
There is an extra carry left over on this one. This is called overflow. It means that the two
(in this case) 8-bit numbers added together need more than 8 bits. They need 9. Show this in
the examination to make it clear you know what has happened. You may also be asked what it is
and why it is there.
5 Binary numbers 35
B Guided questions
5 Binary numbers
Copy out the workings and complete the answers on a separate piece of paper.
1 Add the binary numbers 0110 and 1001.
Step 1: put the binary numbers together.
0 1 1 0
1 0 0 1
1
Step 2: work out the rightmost column. 0 + 1 = 1 (Rule 2). Write it in the table.
Step 3: work out the second column. Write it in the table.
Step 4: work out the third column and write it in the table.
Step 5: work out the fourth column and write it in the table.
Step 6: is there an overflow? If so, make sure you include it.
2 Add the binary numbers 01101110 and 11111111.
Step 1: put the binary numbers together.
0 1 1 0 1 1 1 0
C Practice questions
3 Add the binary numbers 01101 and 10110.
4 Add the binary numbers 01110001 and 10001111.
5 Add the binary numbers 10011110 and 11101101.
6 Add the binary numbers 11110110 and 10011111.
7 Add the binary numbers 00001111 and 11111111.
8 Add the binary numbers 100111011011 and 011110111011.
5 Binary numbers
binary numbers, you can split them up and add two, then the third. Or, if you follow
the patterns above, four 1s equal the number 4. 4 in binary is 100. So put a 0, and
carry the 1 across two columns to the left.
0 0 0 1 1 1 1 1
0 1 0 0 0 1 1 1
1 0 0 0 1 0 1 1
1
1
Put a 0 in the box, and carry to 1 two places to the left.
0 0 0 1 1 1 1 1
0 1 0 0 0 1 1 1
1 0 0 0 1 0 1 1
0 1
1 1
Then continue with the next column.
Binary multiplication
This section will show you how to multiply two unsigned binary integers together.
Remember that unsigned means the binary integer is positive and the most significant bit
(leftmost bit) counts as a value.
As with binary addition, there are rules for binary multiplication.
Rule 1: 0×0=0 Rule 2: 1×0=0 Rule 3: 1×1=1
An easy way to remember these rules is that if there is a 0 involved, the answer is 0.
Multiplication then works in the same way as decimal multiplication.
A Worked examples
Let’s have a go with two digits first.
a Multiply the binary numbers 11 and 10.
Step 1: put the numbers in a table.
1 1
1 0
5 Binary numbers 37
Step 2: multiply the top row by the bottom right number. In this case, 0.
5 Binary numbers
1 1 1 1
1 0 1 0
0 0 0
1×0=0 1×0=0
Step 3: add a 0 on the next row.
1 1
1 0
0 0
0
Step 4: multiply the top row by the next number on row 2. In this case, 1. The answers go to the left
of the 0 you just added.
1 1 1 1
1 0 1 0
0 0 0 0
1 0 1 1 0
1×1=1 1×1=1
Step 5: add the results of the multiplications together.
1 1
1 0
0 0
1 1 0
1 1 0
1 0 1
1 0 1
5 Binary numbers
1 0 1 1 0 1 1 0 1
1 0 1 1 0 1 1 0 1
1 0 1 1 0 1
1×1=1 0×1=0 1×1=1
Step 3: add a 0 on the next row.
1 0 1
1 0 1
1 0 1
0
Step 4: multiply the top row by the second number on row 2. In this case, 0. The answers go to the
left of the 0 you added in Step 3.
1 0 1 1 0 1
1 0 1 1 0 1
1 0 1 1 0 1
0 0 0 0 0
1×0=0 0×0=0
1 0 1
1 0 1
1 0 1
0 0 0 0
1×0=0
Step 5: add two 0s on the next row.
1 0 1
1 0 1
1 0 1
0 0 0 0
0 0
Each time you move across one bit on the bottom row, you add another 0. So for the
first bit (the rightmost), you add no 0s. For the second, you add one 0. For the third,
you add two 0s. For the fourth, you add three 0s, etc.
5 Binary numbers 39
Step 6: multiply the top row by the third number on row 2. In this case, 1.
5 Binary numbers
1 0 1 1 0 1
1 0 1 1 0 1
1 0 1 1 0 1
0 0 0 0 0 0 0 0
1 0 0 0 1 0 0
1×1=1 0×1=0
1 0 1
1 0 1
1 0 1
0 0 0 0
1 0 1 0 0
1×1=1
Step 7: now you have multiplied all of the top row by all of the bottom row, add together the
resulting numbers.
1 0 1
1 0 1
1 0 1
0 0 0 0
1 0 1 0 0
1 1 0 0 1
1
When adding binary numbers, remember to work from right to left one column at
a time. Check which rule it meets, and don’t forget to include carries. If you do not
want to add all three numbers together, add the first two, then add the third to it.
5 Binary numbers
Copy out the workings and complete the answers on a separate piece of paper.
1 Multiply the binary numbers 111 and 001.
Step 1: put the numbers in a table.
1 1 1
0 0 1
1
Step 2: multiply the top row by the bottom right number. Write the result of the
calculation in the boxes of the main table. The first one has been filled in for you.
1 1 1 1 1 1 1 1 1
0 0 1 0 0 1 0 0 1
1 1 1 1 1 1 1 1 1
0 0 1 0 0 1 0 0 1
Step 6: multiply the top row by the third number on row 2. The answers go to the left
of the 0s you added in Step 5.
1 1 1 1 1 1 1 1 1
0 0 1 0 0 1 0 0 1
Step 7: now you have multiplied all of the top row by all of the bottom row, add
together the resulting numbers.
When adding binary numbers, remember to work from right to left one
column at a time. Check which rule it meets, and don’t forget to include
carries. If you do not want to add all three numbers together, add the first two,
then add the third to it.
5 Binary numbers 41
5 Binary numbers
0 0 1 1 1
1 0 0 1 1
Step 2: multiply the top row by the bottom right number. Write the result of the
calculation in the boxes in the main table.
0 0 1 1 1 0 0 1 1 1 0 0 1 1 1
1 0 0 1 1 1 0 0 1 1 1 0 0 1 1
0 0 1 1 1 0 0 1 1 1
1 0 0 1 1 1 0 0 1 1
0 0 1 1 1 0 0 1 1 1 0 0 1 1 1
1 0 0 1 1 1 0 0 1 1 1 0 0 1 1
0 0 1 1 1 0 0 1 1 1
1 0 0 1 1 1 0 0 1 1
5 Binary numbers
a time. Check which rule it meets, and don’t forget to include carries. If you do not
want to add all three numbers together, add the first two, then add the next to it.
Continue until they are all added together.
C Practice questions
3 Multiply the binary numbers 010 and 111.
4 Multiply the binary numbers 1100 and 0110.
5 Multiply the binary numbers 10111 and 01111.
6 Multiply the binary numbers 11000 and 00011.
7 Multiply the binary numbers 101100 and 0111.
8 Multiply the binary numbers 11110110 and 1101.
9 Multiply the binary numbers 10111110 and 0001101.
10 Multiply the binary numbers 10110101 and 00111101.
A Worked examples
a Convert the sign and magnitude number 01010111 into a decimal number.
Step 1: work out the decimal, ignoring the sign bit. (0)1010111 = 87
Step 2: is the first bit (in brackets in Step 1) a 1 or 0? If 1, add a ‘–’. It’s a 0, so the number is positive.
As a decimal the number is 87.
b Convert the sign and magnitude number 10111110 into a decimal number.
Step 1: work out the decimal, ignoring the sign bit. (1)0111110 = 62
Step 2: is the first bit a 1 or 0? It’s a 1, so the number is negative. As a decimal the number is –62.
5 Binary numbers 43
5 Binary numbers
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
1 Convert the sign and magnitude number 01100010 into a decimal number.
Step 1: work out the decimal, ignoring the first bit. (0)1100010 =
Step 2: is the first bit (in brackets) a 1 or a 0? If a 1, the number is negative, if a 0,
it’s positive.
2 Convert the sign and magnitude number 10001101 into a decimal number.
Step 1: work out the decimal, ignoring the first bit.
Step 2: is the first bit a 1 or a 0? Is the number positive or negative?
3 Convert the decimal number 68 into a sign and magnitude number.
Step 1: work out the base 2 value.
Step 2: is the number positive or negative? If positive, add a 0 to the front, if
negative, add a 1.
4 Convert the decimal number –102 into a sign and magnitude number.
Step 1: work out the base 2 value.
Step 2: is the number positive or negative? Add a 0 or a 1 to the front.
C Practice questions
5 Convert the sign and magnitude number 01011 into a decimal number.
6 Convert the sign and magnitude number 11001 into a decimal number.
7 Convert the sign and magnitude number 01011001 into a decimal number.
8 Convert the sign and magnitude number 10111011 into a decimal number.
9 Convert the decimal number 55 into a sign and magnitude number.
10 Convert the decimal number –55 into a sign and magnitude number.
11 Convert the decimal number 150 into a sign and magnitude number.
12 Convert the decimal number –174 into a sign and magnitude number.
5 Binary numbers
In two’s complement, positive binary numbers are represented in the usual way.
Negative numbers are represented in a way that, in computing, behaves like the
negative of the original number.
A Worked examples
a Convert the denary number –35 into two’s complement.
Step 1: write +35 in binary (add 0 at the left to show it is positive).
0100011
Step 2: is the number you are converting negative? Yes, so flip every bit.
1011100
Step 3: add 1.
1 0 1 1 1 0 0
+ 0 0 0 0 0 0 1
1 0 1 1 1 0 1
5 Binary numbers 45
c Convert the denary number 200 into two’s complement.
5 Binary numbers
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
1 Convert the denary number 40 into two’s complement.
Step 1: write +40 in binary (make sure it is positive).
0101000
Step 2: is the number you are converting negative? No, so double check there is a 0 at
the front.
Step 3: write the answer.
2 Convert the denary number –83 into two’s complement.
Step 1: write +83 in binary (make sure it is positive).
01010011
Step 2: is the number you are converting negative? Yes, so flip every bit.
Step 3: add 1.
Step 4: write the answer.
3 Convert the denary number –200 into two’s complement.
Step 1: write +200 in binary (make sure it is positive).
Step 2: is the number you are converting negative? Yes, so flip every bit.
Step 3: add 1.
Step 4: write the answer.
C Practice questions
4 Convert the denary number +55 into two’s complement.
5 Convert the denary number +202 into two’s complement.
6 Convert the denary number –13 into two’s complement.
7 Convert the denary number –99 into two’s complement.
8 Convert the denary number +123 into two’s complement.
9 Convert the denary number –123 into two’s complement.
5 Binary numbers
11 Convert the denary number –302 into two’s complement.
12 Convert the denary number +321 into two’s complement.
13 Convert the denary number –400 into two’s complement.
A Worked examples
a Convert the two’s complement number 1010 into denary.
Step 1: is the number you are converting negative? Yes.
Step 2: from right to left, write all the bits up to and including the first 1 and flip the rest.
Before 1 0 1 0
After 0 1 1 0
Before 1 1 0 1 1 0
After 0 0 1 0 1 0
Step 3: treat it as a positive binary number and convert it to denary, 001010 = 10.
Step 4: write the answer with a ‘–’, –10.
c Convert the two’s complement number 010110 into denary.
Step 1: is the number you are converting negative? No.
Step 2: convert it as a positive binary number, 010110 = 22.
5 Binary numbers 47
B Guided questions
5 Binary numbers
Copy out the workings and complete the answers on a separate piece of paper.
1 Convert the two’s complement number 01011 into denary.
Step 1: is the number you are converting negative? No.
Step 2: convert it as a positive binary number.
2 Convert the two’s complement number 100010 into denary.
Step 1: is the number you are converting negative? Yes.
Step 2: from right to left, write all the bits up to and including the first 1 and flip the rest.
Step 3: treat it as a positive binary number and convert it to denary.
Step 4: write the answer with a ‘–’.
3 Convert the two’s complement number 11110001 into denary.
Step 1: is the number you are converting negative?
Step 2: from right to left, write all the bits up to and including the first 1 and flip the
rest.
Step 3: treat it as a positive binary number and convert it to denary.
Step 4: write the answer with a ‘–’.
C Practice questions
4 Convert the two’s complement number 010101 into denary.
5 Convert the two’s complement number 11011000 into denary.
6 Convert the two’s complement number 10110010 into denary.
7 Convert the two’s complement number 01011011 into denary.
8 Convert the two’s complement number 0101111101 into denary.
9 Convert the two’s complement number 1110001010 into denary.
10 Convert the two’s complement number 1011010101 into denary.
11 Convert the two’s complement number 0101100001 into denary.
12 Convert the two’s complement number 0101111001010100 into denary.
13 Convert the two’s complement number 1010011001001010 into denary.
5 Binary numbers
What is the range of numbers that can be stored in two’s complement with 5 bits?
2n−1 −1 = 25−1 − 1 = 24 − 1 = 16 − 1 = 15
The range is –15 to +15.
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
1 What is the range of numbers that can be stored in two’s complement with 10 bits?
Copy the line below and fill in the gaps. Remember that n is the number of bits (10).
2n−1 − 1 = 2 −1= −1= =
2 What is the range of numbers that can be stored in two’s complement with 12 bits?
Copy the line below and fill in the gaps
2n−1 − 1 = 2 −1= −1=
C Practice questions
3 What is the range of numbers that can be stored in two’s complement with 3 bits?
4 What is the range of numbers that can be stored in two’s complement with 7 bits?
5 What is the range of numbers that can be stored in two’s complement with 11 bits?
6 What is the range of numbers that can be stored in two’s complement with 13 bits?
7 What is the range of numbers that can be stored in two’s complement with 16 bits?
A Worked examples
a Perform the binary subtraction of 12 – 3.
Step 1: turn it into an addition. 12 + –3.
Step 2: turn the first number into binary (make sure it is positive with a 0 at the front). 12 = 01100
Step 3: turn the second number into two’s complement.
+3 = 0011
−3 = 1101
Step 4: put the two numbers in a table ready to add together. Make sure they both have the same number
of bits by adding 0s at the front of the positive number, or 1s at the front of the negative number.
5 Binary numbers 49
5 Binary numbers
+12 0 1 1 0 0
–3 1 1 1 0 1
0 1 0 0 1
1 1 1
There will be an overflow, but you don’t need to worry about it when subtracting. Just ignore it.
The answer is 01001.
b Perform the binary subtraction of 53 – 15.
Step 1: turn it into an addition. 53 + –15
Step 2: turn the first number into binary. 53 = 0110101
Step 3: turn the second number into two’s complement.
+15 = 01111
−15 = 10001
Step 4: put the numbers in a table and add them. (Fill in any gaps so they are the same length.)
+53 0 1 1 0 1 0 1
–15 1 1 1 0 0 0 1
0 1 0 0 1 1 0
1 1 1 1
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
1 Perform the binary subtraction 31 – 4.
Step 1: turn it into addition. 31 + −4
Step 2: turn the first number (31) into binary.
Step 3: turn the second number (–4) into two’s complement (binary first, then flip
and add 1).
Step 4: put the numbers in a table and add them.
2 Perform the binary subtraction 99 – 45.
Step 1: turn it into addition.
Step 2: turn the first number into binary.
Step 3: turn the second number into two’s complement.
Step 4: put the numbers in a table and add them.
5 Binary numbers
3 Perform the binary subtraction 10 – 2.
4 Perform the binary subtraction 50 – 6.
5 Perform the binary subtraction 101 – 12.
6 Perform the binary subtraction 155 – 4.
7 Perform the binary subtraction 200 – 200.
8 Perform the binary subtraction 212 – 99.
9 Perform the binary subtraction 200 – 197.
10 Perform the binary subtraction 230 – 19.
11 Perform the binary subtraction 255 – 184.
12 Perform the binary subtraction 356 – 287.
Fixed point
It is possible to store denary, or fractional, numbers in binary. This is usually called fixed
point binary. It includes a point separating the whole-number part and the fractional part.
The binary digits after the point are represented by 2−1, 2−2, etc. from left to right.
.
× 23 × 22 × 21 × 20 . × 2–1 × 2–2
5 Binary numbers 51
A Worked example
5 Binary numbers
A Worked example
Convert the denary number 20.34375 to fixed point.
Step 1: write the binary for 20 = 10100.
Step 2: multiply the fractional part (0.34375) by 2: 0.34375 × 2 = 0.6875
Then multiply the fractional part (0.6875) by 2: 0.6875 × 2 = 1.375
Continue multiplying each fractional part by 2: 0.375 × 2 = 0.75
0.75 × 2 = 1.5
0.5 × 2 = 1.0
Step 3: write the binary for the fraction (top to bottom): 01011
So the answer is 10100.01011
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
1 Convert the fixed point number 0110.11 to denary.
Step 1: copy the table and write the bits in the first row. The integer part has been
done for you.
0 1 1 0 .
× 23 × 22 × 21 × 20 . × 2–1 × 2–2
0 4
Step 2: write the results of each multiplication. The first two have been done for you.
Step 3: add the results.
5 Binary numbers
Step 1: copy the table and write the bits in the first row.
.
× 27 × 26 × 25 × 24 × 23 × 22 × 21 × 20 . × 2–1 × 2–2 × 2–3 × 2–4
Step 2: write the results of each multiplication in the third row of the table.
Step 3: add the results.
3 Convert the denary number 13.125 to fixed point.
Step 1: write the binary for 13.
Step 2: multiply the fractional part (0.125) by 2: ×2=
Then multiply the fractional part by 2: ×2=
×2=
Step 3: write the binary for the fraction (top to bottom):
The answer is
4 Convert the denary number 6.3125 to fixed point.
Step 1: write the binary for 6.
Step 2: multiply the fractional part (0.3125) by 2: ×2=
Then multiply the fractional part by 2: ×2=
×2=
×2=
Step 3: write the binary for the fraction (top to bottom):
The answer is
C Practice questions
5 Convert the fixed point number 0101.111 to denary.
6 Convert the fixed point number 11011.1001 to denary.
7 Convert the fixed point number 110111.001 to denary.
8 Convert the fixed point number 111001.01011 to denary.
9 Convert the fixed point number 10101010.11001 to denary.
10 Convert the denary number 5.125 to fixed point.
11 Convert the denary number 12.625 to fixed point.
12 Convert the denary number 20.75 to fixed point.
13 Convert the denary number 36.3125 to fixed point.
14 Convert the denary number 45.4296875 to fixed point.
5 Binary numbers 53
Logical shifts
5 Binary numbers
Left shift
A left shift involves moving all the bits to the left, and adding 0s on the right. If it’s a
2-place shift, you add two 0s to the right-hand side. If there is a binary point, then you
move the binary point to the right.
Each shift to the left means the number has been multiplied by 2. A 2-place shift means
the number is multiplied by 4, a 3-place shift means the number is multiplied by 8.
If a question asks for the number to remain a set number of bits, e.g. an 8-bit number,
then you would need to remove bits on the left. For example:
0 1 1 0 1 0 1 0
1 1 0 1 0 1 0 0
A Worked examples
a Perform a 2-place left shift on the binary number 00011011.
Add two 0s on the right-hand side: 0001101100
b Perform a 3-place left shift on the binary number 000110.11.
Move the binary point three places to the right. If more bits are needed, add 0s. 000110110
c Show the effect of a 3-place left shift on the binary number 010110110.
Step 1: work out the denary value before the shift: 010110110 = 182
Step 2: add three 0s on the right-hand side: 010110110000
Step 3: work out the denary value after the shift: 010110110000 = 1456
This is a 3-place shift, so the number is multiplied by 8, 182 × 8 = 1456
d Perform a 4-place left shift on the binary number 10010110. Give the result as an 8-bit number.
Step 1: add four 0s on the right-hand side: 100101100000
Step 2: remove 4 bits from the front of the number to make it 8 bits: 01100000
Right shift
A right shift involves moving all the bits to the right. You need to add the binary point
before you make the move, as this will move to the left.
Each shift to the right means the number has been divided by 2. A 2-place shift means
the number is divided by 4, a 3-place shift means the number is divided by 8.
5 Binary numbers
a Perform a 2-place right shift on the binary number 01101010.
Step 1: add the binary point: 01101010.
Step 2: move the binary point two places to the left: 011010.10
b Perform a 3-place right shift on the binary number 10110101.
Step 1: add the binary point: 10110101.
Step 2: move the binary point three places to the left: 10110.101
c Show the effect of a 2-place right shift on the binary number 01011010.
Step 1: work out the denary value before the shift: 01011010 = 90
Step 2: add the binary point: 01011010.
Step 3: move the binary point two places to the left: 010110.10
Step 4: work out the denary value after the shift: 010110.10 = 22 12
This is a 2-place shift, so the number is divided by 4, 90 ÷ 4 = 22 12
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
1 Perform a 1-place left shift on the binary number 10101101.
Add one 0 on the right-hand side.
2 Perform a 1-place right shift on the binary number 01100101.
Step 1: add the binary point: 01100101.
Step 2: move the binary point one place to the left.
3 Perform a 3-place left shift on the binary number 0101101.1.
Move the binary point three places to the right. Add more 0s if needed.
4 Perform a 4-place right shift on the binary number 01011111.
Step 1: add the binary point.
Step 2: move the binary point four places to the left.
5 State the effect of a 3-place left shift on a binary number.
Each shift left multiplies it by 2, so a 1-shift is multiplied by 2, a 2-shift is multiplied
by 4, and a 3-shift is
6 State the effect of a 4-place right shift on a binary number.
Each shift right divides by 2, so a 1-shift divides the number by 2, a 2-shift
divides the number by 4, a 3-shift divides the number by 8, a 4-shift divides the
number by
5 Binary numbers 55
C Practice questions
5 Binary numbers
Bitwise operators
Note: this topic is assessed at A-level only.
There are four operators that you need to be able to apply to either one or more binary
numbers. These are NOT, OR, AND and XOR. They act in the same way as in logic
gates and binary logic.
NOT
A NOT function changes a 0 to a 1, and a 1 to a 0.
AND
An AND function takes two binary numbers. If they are both 1, the answer is 1. If either
is 0, the answer is 0.
OR
An OR function takes two binary numbers. If one or both are 1, the answer is 1. If both
are 0, the answer is 0.
XOR
XOR (exclusive OR) takes two binary numbers. If they are both 0, or both 1, the result is
0. If only one is a 1, the answer is 1.
When the AND, OR or XOR are applied, you need two binary numbers. One of these
may be referred to as a ‘mask’. This just means it is the binary number you are using to
complete the operation.
5 Binary numbers
a Perform the bitwise NOT operation on 1011.
Reverse each of the bits to give 0100.
b Perform a bitwise AND operation on 1011 with the mask 0111.
Step 1: put the numbers above each other, like in an addition.
1 0 1 1
0 1 1 1
0 0 1 1
Step 2: compare each pair of bits in turn. If they are both 1, the result is 1, otherwise the result is 0.
c Perform a bitwise OR operation on 1011 with the mask 0111.
Step 1: put the numbers above each other.
1 0 1 1
0 1 1 1
1 1 1 1
Step 2: compare each pair of bits in turn. If either or both are 1s, the result is 1, otherwise the result
is 0.
d Perform a bitwise XOR operation on 1011 with the mask 0111.
Step 1: put the numbers above each other.
1 0 1 1
0 1 1 1
1 1 0 0
Step 2: compare each pair of bits in turn. If both are 0s or 1s, the result is 0, otherwise the result is 1.
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
1 Perform a bitwise NOT operation on 01011011.
Reverse each of the bits.
2 Perform a bitwise AND operation on 010110 with the mask 101101.
Step 1: put the numbers above each other, like in an addition.
0 1 0 1 1 0
1 0 1 1 0 1
0 0
Step 2: compare each pair of bits in turn. If they are both 1, the result is 1, otherwise
the result is 0.
5 Binary numbers 57
3 Perform a bitwise OR operation on 10101110 with the mask 00110110.
5 Binary numbers
1 0 1 0 1 1 1 0
0 0 1 1 0 1 1 0
1 0
Step 2: compare each pair of bits in turn. If either or both are 1s, the result is 1,
otherwise the result is 0.
4 Perform a bitwise XOR operation on 10101101 with the mask 10100010.
Step 1: put the numbers above each other.
1 0 1 0 1 1 0 1
1 0 1 0 0 0 1 0
0 0
Step 2: compare each pair of bits in turn. If both are 0s or 1s, the result is 0, otherwise
the result is 1.
C Practice questions
5 Perform a bitwise NOT operation on 10101101.
6 Perform a bitwise NOT operation on 01000110.
7 Perform a bitwise AND operation on 01011011 with the mask 10111111.
8 Perform a bitwise AND operation on 10111101 with the mask 00001000.
9 Perform a bitwise OR operation on 00101011 with the mask 10111011.
10 Perform a bitwise OR operation on 11101111 with the mask 10111010.
11 Perform a bitwise XOR operation on 01000010 with the mask 10101011.
12 Perform a bitwise XOR operation on 11101111 with the mask 01101100.
Table 6.1
1 1 1 0 . 1 1
× 23 × 22 × 21 × 20 × 2–1 × 2–2
Another method is to note that the value of each column to the left of the binary
point doubles in value, from 1 to 2, then 4 and so on. To the right of the binary point,
each column halves in value, from 0.5 to 0.25, then 0.125 and so on. This may be
easier than working out powers of 2 every time!
However, it is inefficient to store very small or very large numbers in this way, so a
system called floating point binary is used instead.
a Calculate the denary value of the floating point binary number 0010101010110, where 9 bits
are used for the mantissa followed by 4 bits for the exponent, both in two’s complement.
Step 1: split the binary number into the mantissa (0.01010101) and the exponent (0110).
Step 2: calculate the denary value of the exponent. 0110 = 6
Step 3: move the binary point in the mantissa six places to the right. 0010101.01
Step 4: the mantissa is now 0010101.01 — converting to denary gives (16 + 4 + 1 + 0.25) = 21.25
b Calculate the denary value of the floating point binary number 011110, where 3 bits are used
for the mantissa followed by 3 bits for the exponent, both in two’s complement.
Step 1: split the binary number into the mantissa (0.11) and the exponent (110).
Step 2: calculate the denary value of the exponent. 110 = –4 + 2 = –2
Step 3: because the exponent is negative, the binary point in the mantissa is moved left two places
instead of right.
Step 4: the mantissa is now 0.0011 (added zeros are needed when moving the point). Converting to
denary gives: 0.125 + 0.0625 = 0.1875
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
1 Calculate the denary value of the floating point binary number 1011001011,
where 7 bits are used for the mantissa followed by 3 bits for the exponent, both
in two’s complement.
Step 1: split the binary number into the mantissa (7 bits) and the exponent (3 bits).
Step 2: calculate the denary value of the exponent.
Step 3: in this case, the answer from Step 2 is positive, so move the binary point in
the mantissa this many places right.
Step 4: fill in your answer from Step 3 in a table like the one below. Use this to
calculate the denary value. Don’t forget that two’s complement is being used, so the
leftmost bit is negative.
.
× −23 × 22 × 21 × 20 × 2−1 × 2−2 × 2−3
2 Calculate the denary value of the floating point binary number 010111, where
4 bits are used for the mantissa followed by 2 bits for the exponent, both in two’s
complement.
Step 1: split the binary number into the mantissa (4 bits) and the exponent (2 bits).
Step 2: calculate the denary value of the exponent. Remember, two’s complement is
being used.
.
× −20 × 2−1 × 2−2 × 2−3 × 2−4
C Practice question
3 Calculate the denary values of the following floating point binary numbers.
All values are given in two’s complement.
a 010110010101 (8 bits mantissa, 4 bits exponent)
b 100111110110 (8 bits mantissa, 4 bits exponent)
c 0111001010 (6 bits mantissa, 4 bits exponent)
d 0101101110101 (9 bits mantissa, 4 bits exponent)
e 1011111010 (4 bits mantissa, 6 bits exponent)
f 0111100101010 (8 bits mantissa, 5 bits exponent)
g 010111010000 (6 bits mantissa, 6 bits exponent)
h 10100011001100 (6 bits mantissa, 6 bits exponent)
Normalisation
In this context, normalisation is the process of maximising the precision of a number
when storing it in a given number of bits. To do this for a positive binary number involves
removing any leading zeros. To do the same for a negative binary number involves
removing any leading ones. This means that a normalised floating point number must
always start as either 0.1 or 1.0.
Normalisation is also the name given to the process of simplifying the design of
relational databases. This is completely unrelated — do not get confused!
To normalise a floating point binary number, you simply move the binary point in the
mantissa right or left until the above is true. The exponent is then decreased (if the binary
point is moved right) or increased (if moved left) by the number of places moved.
a The floating point binary number 000101010101 has an 8-bit mantissa followed by a 4-bit
exponent, both in two’s complement. Find the normalised version of this number.
Step 1: split the binary number into the mantissa (0.0010101) and the exponent (0101).
Step 2: move the binary point in the mantissa right until it starts as 0.1 or 1.0. Moving it two places
ensures that the mantissa is now 0.10101.
Step 3: however, the mantissa is now only 6 digits in length, so add two zeros to the right-hand side.
This keeps it at 8 digits (as required by the question) but does not change the value. The mantissa is
now 0.1010100.
Step 4: as the binary point has been moved two places right, the exponent must be decreased by 2.
The exponent is currently 0101 (5 in denary), so subtracting 2 from this gives 0011 (3).
Step 5: join together the new mantissa and exponent in the format given in the question. In this
case, the normalised value is 010101000011.
b Give the denary value 27.75 as a normalised floating point binary number, using 8 bits for the
mantissa and 4 bits for the exponent, both in two’s complement.
Step 1: write 27.75 as a fixed point binary number using two’s complement = 011011.11.
Step 2: move the binary point five places to the left so that the mantissa is normalised (giving
0.1101111).
Step 3: the binary point was moved five places left, so increase the exponent by 5. (The exponent is
currently 0, so 0 + 5 = 5.) This is 0101 in two’s complement binary.
Step 4: join together the new mantissa and exponent in the format given in the question. In this
case, the normalised value is 011011110101.
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
1 State whether each of these floating point binary numbers is normalised or not:
a 1011011011
b 0011001010
c 1010010100
■ To be normalised, a floating point number must start with 10 or 01.
■ Identify which numbers above meet this pattern. These are normalised. Any
numbers starting 00 or 11 are not normalised.
2 The floating point binary number 0011110011 has a 7-bit mantissa followed by
a 3-bit exponent, both in two’s complement. Find the normalised version of this
number.
Step 1: split the binary number into the mantissa (7 bits) and the exponent (3 bits). Put
the binary point between the first two digits of the mantissa.
Step 2: move the binary point in the mantissa right until it starts as 0.1, trimming off
any leading zeros. Note how many places this had to be moved.
C Practice questions
4 State whether each of these floating point binary numbers is normalised or not:
a 0010110101
b 1101011010
c 0110110101
5 Give the normalised versions of each of the following floating point binary numbers.
Both the mantissa and exponent are in two’s complement.
a 001010000100 (8 bits mantissa, 4 bits exponent)
b 000010110111 (8 bits mantissa, 4 bits exponent)
c 1101000011 (6 bits mantissa, 4 bits exponent)
d 1010010 (4 bits mantissa, 3 bits exponent)
e 001101011001 (8 bits mantissa, 4 bits exponent)
6 Give the normalised floating point binary version of the following denary numbers,
using two’s complement, 8 bits for the mantissa and 4 bits for the exponent.
a 11.375
b 6.875
two’s complement.
A Worked examples
a Calculate the largest number that can be stored using an 8-bit binary number if the number
uses 3 bits for the mantissa and 5 bits for the exponent.
Step 1: with 3 bits for the mantissa and 5 bits for the exponent, then the largest number that can be
represented is 01101111.
Step 2: the mantissa is therefore 0.11, with the binary point needing to be moved 15 places to the
right (the exponent 01111 = 15).
Step 3: convert to denary. 0110000000000000 = 8192 + 16 384 = 24 576. This means that the largest
denary number that can be stored in this format is 24 576.
b Calculate the largest number that can be stored using an 8-bit binary number if the number
uses 5 bits for the mantissa and 3 bits for the exponent.
Step 1: with 5 bits for the mantissa and 3 bits for the exponent, the largest number that can be
represented is 01111011.
Step 2: the mantissa is therefore 0.1111, with the binary point needing to be moved three places to the
right (011 = 3). This means that the largest denary number that can be stored in this format is 7.5.
Step 3: convert to denary. 0111.1 = 7.5.
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
1 Calculate the largest number that can be stored using a 12-bit binary number
if the number uses 8 bits for the mantissa and 4 bits for the exponent.
Step 1: write down the maximum value that can be stored in the mantissa and the
exponent. Because of two’s complement, this will be entirely 1s, except for the
first digit of each, which will be 0.
Step 2: insert the binary point correctly into the mantissa and then move this to the
right the number of times indicated by the exponent.
Step 3: convert to denary.
2 Calculate the largest number that can be stored using a 12-bit binary number if
the number uses 7 bits for the mantissa and 5 bits for the exponent.
Step 1: write down the maximum value that can be stored in the mantissa and the exponent.
Step 2: insert the binary point correctly into the mantissa and then move this as needed.
Step 3: convert to denary.
Errors
Because of the trade-off between range and precision, the representation of some floating
point binary numbers may not always be as precise as we would like. For example, if the
number 76.65625 were to be stored, the full representation would be 0100110010101 for
the mantissa (13 bits) and 0111 for the exponent (4 bits). If, however, only 10 bits were
made available for the mantissa, then this would effectively lose the three leftmost bits,
leaving us with a representation of 76.625. This is very close, but not as precise as before.
This error can be described as an absolute or relative error. The absolute error is the
difference in value between the original number and the representation. This is found by
subtracting one from the other. The relative error is the absolute error expressed as a
percentage of the true value.
A Worked examples
a When the number 76.65625 is represented using 10 bits for the mantissa and 4 bits for the
exponent, the actual value stored is 76.625 (see above for further explanation). Calculate the
absolute and relative errors.
Step 1: the absolute error is the difference between the two values. 76.65625 – 76.625 = 0.013125
Step 2: to find the relative error, find the absolute error as a percentage of the true value. To do this
0.013125
use the formula × 100. This is × 100 = 0.017%
absolute error
true value 76.65625
b Find the absolute and relative errors when the value 3366 is represented using 8 bits for the
mantissa and 5 bits for the exponent.
Step 1: find the fixed point binary representation of 3366. This is 0110100100110 (there are no
fractional parts yet, so the binary point would be to the right-hand side of the rightmost bit).
Step 2: normalise this number and work out the mantissa and exponent. This gives a mantissa of
0.110100100110 and an exponent of 12, which is 01100.
Step 3: to fit the 13-bit mantissa into the 8 bits available, the rightmost 5 bits will need to be lost.
This will give us a new mantissa of 0.1101001.
Step 4: the new mantissa is 0.1101001 and the exponent is 01100. Calculate the value of this in
denary, which is 3360.
Step 5: the absolute error is 3366 – 3360 = 6
6
Step 6: the relative error is 3366 × 100 = 0.178%
Copy out the workings and complete the answers on a separate piece of paper.
1 a Find the absolute and relative errors when the value 109.5 is represented using
8 bits for the mantissa and 4 bits for the exponent.
Step 1: find the fixed point binary representation of 109.5.
Step 2: normalise this number and therefore work out the mantissa and the
exponent.
Step 3: cut down the mantissa so that it fits into the 8 bits given in the question by
removing digits from the right-hand side.
Step 4: calculate the new value of the number in denary, using the new mantissa
and the exponent.
Step 5: find the absolute error by subtracting the new value from the true value.
absolute error
Step 6: find the relative error by using true value × 100.
b Find the absolute and relative errors when the value 109.5 is represented using
4 bits for the mantissa and 4 bits for the exponent.
Steps 1 and 2: this uses the same true value as in part a, so find the normalised
version of this from Step 2 of part a.
Step 3: this time, trim down the mantissa to fit into 4 bits.
Step 4: calculate the new value of the number in denary, using the new mantissa
and the exponent.
Step 5: find the absolute error by subtracting the new value from the true value.
absolute error
Step 6: find the relative error by using true value
× 100.
C Practice questions
2 Find the absolute and relative errors when:
a 178.56 is stored as 178
b 56 564 is stored as 56 500
c 0.0000034 is stored as 0.000003
3 Find the absolute and relative errors when the value 47.75 is represented using 6 bits
for the mantissa and 4 bits for the exponent.
4 Find the absolute and relative errors when the value 30 079 is represented using 6 bits
for the mantissa and 5 bits for the exponent.
Bitmap images
A bitmap image is made up of many smaller blocks, called pixels. Each pixel is assigned
a single colour, with the computer storing a bit pattern to represent this colour. As these
single-colour pixels are so small, the human eye interprets the bitmap as a complete picture.
order matters. Really, a combination lock should be called a permutation lock. The
number of permutations of a set of values of size n is n! (read ‘n factorial’). For example,
a set of 5 letters can be arranged in 5! ways (5 × 4 × 3 × 2 × 1 = 120 ways).
log ( c )
Conversely, if c colours are need to be stored then will give the number of bits
log (2)
required. If the number given by this equation is not an integer, then rounding up is
necessary to the next whole number.
Once the number of pixels in an image and the number of bits required for each pixel are both
known, these can be multiplied together to find the storage requirements for the image in bits.
This can then be converted to the required units using the methods described in Unit 3.
Metadata
Alongside the raw data about each pixel, some bitmap image formats store information
about the image. This will typically include the resolution and bit depth, but some file
formats such as JPG can also store information about the author, the hardware used to
create the picture or even geographical tagging information. This data about the image is
called metadata and will take up a small amount of additional storage space.
A Worked examples
a A bitmap image is 1500 × 1200 in size. Calculate the resolution of the image in megapixels.
Step 1: calculate the number of pixels in the image by multiplying 1500 × 1200 = 1 800 000 pixels.
Step 2: each megapixel is 1 million pixels, so divide 1 800 000 ÷ 1 000 000 = 1.8.
So the resolution is 1.8 megapixels.
b A bitmap image has resolution 2048 × 512 and contains 23 distinct colours. Calculate the file
size of the image in kilobytes.
Step 1: calculate the number of pixels in the image by multiplying 2048 × 512 = 1 048 576 pixels.
log ( 23)
Step 2: 23 distinct colours needs bits per pixel = 4.524.
log (2)
Step 3: round this up to give the actual number of bits needed per pixel = 5 bits.
Step 4: multiply the number of pixels by the bits per pixel = 1 048 576 × 5 = 5 242 880 bits.
Step 5: convert this to kilobytes:
5 242 880 ÷ 8 = 655 360 bytes
655 360 ÷ 1024 = 640 kilobytes
So the file size is 640 kilobytes.
C Practice questions
5 a A bitmap image has resolution 1000 × 400. Calculate the resolution in megapixels.
b Each pixel in the image is stored using 16 bits. Calculate the file size in bytes.
6 a A bitmap image with resolution 2000 × 5000 uses 400 distinct colours. Calculate
the size of the file in megabytes.
Sound samples
A sound wave is analogue, continuous data. In order for a computer to store a
representation of a sound wave, regular samples of the height of the waveform need to be
taken which are then converted to binary and stored.
The quality of the sound file depends on both the sample resolution and the sample
rate. The higher that both of these are, the better the quality of the audio. As an
example, telephone calls use a resolution of 8 bits and a sample rate of 11 kHz. Music
CDs use 16 bits and 44 kHz.
A Worked examples
a A 30 second sound file has a sample rate of 44 000 Hz using 16 bits per sample. Calculate the file
size in bytes.
■ Remember that there are 8 bits in a byte.
■ A bit is a binary digit, a 1 or a 0.
Step 1: multiply the length (in seconds) by the sample rate (in Hz) and multiply this by the sample
resolution (in bits). This would be 30 × 44 000 × 16 = 21 120 000 bits.
Step 2: divide by 8 to convert to bytes = 2 640 000 bytes.
b A voice recording uses 8 bits per sample and a sample rate of 8 kHz (note: 1 kHz = 1000 Hz). The
recording is stored in a file that is 50 kilobytes in size. Calculate the length of the recording, in
seconds.
Step 1: rearrange the formula
size = length × rate × resolution
to give
size (in bits)
length = rate resolution
×
Step 2: calculate the file size in bits by multiplying by 1024 and then by 8.
50 × 1024 × 8 = 409 600 bits
Step 3: use the formula from Step 1.
409 600 409 600
length = = = 6.4 seconds
8000 × 8 64 000
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
1 An audio file is recorded using a resolution of 16 bits and a sample rate of 48 kHz.
The audio file is 90 seconds long. Calculate the size of the file in megabytes,
giving your answer to 3 decimal places.
Step 1: calculate number of bits: 90 × 16 × 48 000 =
Step 2: convert this to megabytes by dividing by 8 (to obtain bytes), then dividing by
1024 (to obtain kilobytes) then dividing by 1024 again (to obtain megabytes).
Step 3: round to three decimal places.
sample rate of 44 kHz. Calculate the size of the file in megabytes, rounding to the
nearest megabyte.
Step 1: calculate the number of seconds in 5 minutes.
Step 2: use the answer from Step 1 together with the sample resolution (12) and the
sample rate (44 000) to calculate the file size in bits.
Step 3: convert this to megabytes.
Step 4: round to the nearest megabyte.
3 A band records its latest song using a resolution of 16 bits and a sample rate of
44 kHz. It records separate tracks for the singer, the guitar player, the bass player
and the drummer. Each track is 3 minutes long. Calculate the total size of the
files recorded in megabytes, rounding to the nearest megabyte.
Step 1: use the formula given at the start of this section to calculate the file size, in
bits, for one of the audio tracks (remember to use seconds not minutes!).
Step 2: multiply this by 4 (as there are 4 tracks recorded).
Step 3: convert to megabytes and round as needed.
C Practice questions
4 Calculate the file size of the following sound files. Give your answers to 3 decimal
places, using the most appropriate unit.
a Length = 15 seconds, sample resolution = 8 bits, sample rate = 11 500 Hz
b Length = 90 seconds, sample resolution = 16 bits, sample rate = 32 kHz
c Length = 2.5 minutes, sample resolution = 16 bits, sample rate = 44.8 kHz
d Length = 1 hour, sample resolution = 8 bits, sample rate = 8 kHz
5 An audio recording uses 24 bits per sample and a sample rate of 48 kHz. The
recording is stored in a file that is 10 megabytes in size. Calculate the length of the
recording, in seconds.
6 A sound file is 525 kilobytes in size, has a length of 38.4 seconds and has been
recorded using a resolution of 8 bits per sample. Calculate the sample rate used during
the recording.
7 A student records the evening news from the radio each day. Each news broadcast
is exactly 5 minutes long and the student records it using a resolution of 16 bits
and a sample rate of 44 kHz. The files are then uploaded to a website with a limit
of 1 gigabyte on storage. How many broadcasts can the student store in the space
available?
Logic gates
Almost all modern computers use a binary system to represent data, with two
outputs (1 or 0, representing True or False) available for every proposition. A logical
proposition is a statement which is either True or False. For example, the output in a
computer game as to whether a player has collided with an enemy is either 1 (True,
the player and enemy have collided) or 0 (False, they have not collided). There are no
other possible outputs.
Logic gates allow the computer to make decisions based on joining these propositions
together, with three basic gates used: AND, OR and NOT. For example, we could say
that the game is over if a player collides with an enemy AND their energy level is low.
In this case, both of the two propositions would need to be true (output 1) for the ‘game
over’ output to be 1. If either (or both) output is 0 then the ‘game over’ output will be 0.
The outputs for two propositions connected with an AND gate are shown in Table 8.1. This
is called a truth table.
Table 8.1
A: Collided with enemy B: Energy level low Game over (A AND B)
0 0 0
1 0 0
0 1 0
1 1 1
The OR gate is similar, but the output is 1 if the output of either (or both) of the
propositions is 1. NOT is slightly different in that it only takes one input and simply
inverts this. If A = 1 then (NOT A) = 0 and vice versa.
Three slightly more complex gates that can be used are NAND, NOR and XOR. These
are explained in Table 8.2.
Table 8.2
Logic gate Explanation
NAND This is the same as AND, but with the output inverted.
A NAND B is the same as NOT(A AND B).
NOR This is the same as OR, but with the output inverted.
A NOR B is the same as NOT(A OR B).
XOR This is known as an ‘eXclusive OR’ gate. The output is 1 if either
one, but not both, of the inputs is 1. If both inputs are 1 or both inputs
are 0 then the output will be 0. This logic gate is commonly used to
decide if two inputs are different.
8 Boolean algebra 73
A Worked example
8 Boolean algebra
The XOR gate produces a 1 output if either, but not both, of the inputs are 1. The truth table is
therefore:
Table 8.3
A B A XOR B
0 0 0
1 0 1
0 1 1
1 1 0
■ A truth table simply shows all possible permutations of 1 and 0 values for the input variables.
■ In this case, because there are two inputs, there are four permutations as shown above.
■ If there were three inputs, there would be nine permutations and therefore nine rows in the
truth table.
ii NOT (A OR B)
Just like in normal algebra, we work on the contents of the brackets first. The truth table for A OR B
would be:
Table 8.4
A B A OR B
0 0 0
1 0 1
0 1 1
1 1 1
The NOT outside the brackets simply inverts the output, so every 0 output turns to 1 and every
1 output turns to 0. The final truth table is therefore:
Table 8.5
A B A OR B NOT (A OR B)
0 0 0 1
1 0 1 0
0 1 1 0
1 1 1 0
■ Once you are confident with constructing truth tables, you do not need to show the intermediate
steps unless the question specifically asks for it.
■ NOT (A OR B) is equivalent to A NOR B.
8 Boolean algebra
Copy out the workings and complete the answers on a separate piece of paper.
1 Create truth tables for each of the following expressions.
a A OR B
Step 1: create a truth table with all four possible permutations of A and B, with
a column for the output. This is shown below.
Table 8.6
A B A OR B
0 0
1 0
0 1
1 1
Table 8.7
A B NOT B
0 0
1 0
0 1
1 1
Step 3: now use the logic connective NAND to join together the columns A
and NOT B (you can ignore the B column at this point). Remember, NAND is
equivalent to AND with the output reversed. Complete the table below.
Table 8.8
A B NOT B A NAND (NOT B)
0 0
1 0
0 1
1 1
c (A AND B) OR C
Step 1: create a truth table with all nine possible permutations of A, B and C.
Step 2: evaluate the brackets first, so evaluate the expression A AND B for each row in
the truth table. You may wish to add the output as a new column into the truth table.
Step 3: now use OR to connect together the outputs from Step 2 with the values in
the C column in the truth table.
8 Boolean algebra 75
C Practice question
8 Boolean algebra
Boolean algebra
Boolean algebra is a formalised way of representing logical statements. It was introduced
by George Boole, an English mathematician, in his book The Mathematical Analysis of
Logic (1847).
The basic logic gate operations of AND, OR, XOR and NOT are replaced with symbols.
The most common of these symbols are shown in Table 8.9.
Table 8.9 Boolean logic symbols
Logic operation Symbol Example
AND ∧ A∧B (A AND B)
OR ∨ A∨B (A OR B)
NOT ¬ ¬ A (NOT A)
XOR ⊕ A⊕B (A XOR B)
Some other resources or questions may use different symbols (a dot for AND, a plus sign
for OR and a bar over the text for NOT are common), but the symbols in the table above
will be consistently used in this book.
A Worked examples
a A and B are two inputs that are joined together by a NAND gate to give output C. Give the
Boolean algebraic expression for this arrangement.
Step 1: there is no symbol for NAND, but recognise that a NAND gate is equivalent to NOT(AND).
Step 2: replace the NOT and AND with symbols. The Boolean expression is therefore C = ¬(A∧B).
b Create a truth table for the expression A ∨ ¬B.
Table 8.10a
Step 1: create an outline of a truth table showing all four A B A∨¬B
permutations of A and B. This has been done here. 0 0
Step 2: decide what symbols are used in the question. In this 1 0
case, A∨ ¬B is A OR NOT B. 0 1
1 1
Step 3: for each row in the truth table, evaluate this expression Table 8.10b
with the given values of A and B. For example, the first row A B A∨¬B
has A = 0 and B = 0. NOT B would be 1 and so the expression 0 0 1
is 0 OR 1, giving an output of 1. 1 0 1
0 1 0
1 1 1
8 Boolean algebra
Copy out the workings and complete the answers on a separate piece of paper.
1 Let W = ‘I will take a taxi’, X = ‘it is raining’, Y = ‘I have money in my pocket’ and
Z = ‘I am late’. Write the following statement using Boolean logic symbols and the
above statements: ‘I will take a taxi if it is raining or I am late, but only if I have
money in my pocket’.
Step 1: decide which of W, X, Y or Z above is the output.
Step 2: for the remaining three logic statements, any that must all happen should be
joined together with the symbol for AND. If there is a choice of one or the other, then
join these together with the symbol for OR.
Step 3: use brackets to clarify the order for evaluation if necessary.
2 A and B are two inputs that are joined together with an XOR gate. The output
of this is then joined to C with an AND gate to give output X. Give the Boolean
algebraic expression for this arrangement.
Step 1: the output is X, so start off the expression with X =
Step 2: after this, use the XOR symbol to join together A and B.
Step 3: as the output of this is used in the next expression, add brackets around your
answer to Step 2.
Step 4: use the AND symbol to join this to C.
3 Create a truth table for the expression A⊕ ¬B.
Step 1: create an outline of a truth table showing all four permutations of A and B.
Table 8.11
A B A⊕ ¬ B
0 0
1 0
0 1
1 1
Step 2: decide what symbols are used in the question, translating these into AND, OR,
XOR or NOT.
Step 3: if you need to, create extra columns in the table to work out intermediate steps.
However, this may not be necessary depending on how confident you feel.
Step 4: evaluate each row in the truth table according to logic rules. Fill in the output.
C Practice questions
4 Write each of the following logic gate arrangements as a Boolean expression.
a The input A is connected to a NOT gate. The output of this is joined to B with an
AND gate. The output of this is connected to another NOT gate, giving output C.
b Two inputs A and B are connected to a NOR gate. Two more inputs, C and D are
connected to an XOR gate. The output of both of these gates is connected to an
AND gate, giving output E.
8 Boolean algebra 77
5 A student has to have good attendance and hand in all of their homework in order to
8 Boolean algebra
be eligible to pass a course. In addition, they must either score 50%+ on a test or score
70%+ on a piece of coursework.
The logic statement A = (B ∧ C) ∧ (D ∨ E) represents these course criteria.
a If A is defined as ‘the student passes the course’, give definitions for each of B, C,
D and E.
b Create a truth table for this logic statement.
6 Imogen investigates a logic system and defines the output using the expression
¬ (A ∧ B). Another student suggests that the expression should instead be ¬A ∨ ¬B.
Imogen states that both expressions will produce the same output.
Investigate whether Imogen is correct or not by creating and comparing truth tables
for both expressions.
8 Boolean algebra
format. For example, the idempotence laws say that I can reduce ‘I will go to bed if I am
tired or I am tired’ down to just ‘I will go to bed if I am tired’. The inverse laws say that
evaluating ‘it is the weekend and it is not the weekend’ will always give a False output.
One set of laws that is perhaps not so linked to common sense or basic mathematics is
De Morgan’s laws, named after the nineteenth-century British mathematician Augustus
De Morgan. These are listed in the Table 8.13.
A Worked examples
a Simplify the expression A ∨ (A ∧ B) ∨ ¬A.
Step 1: use the absorption laws to replace A ∨ (A ∧ B) with A.
Step 2: the expression is now A ∨ ¬A.
Step 3: the inverse law states that A ∨ ¬A = 1. The expression will therefore always output 1.
b Simply the expression (¬A ∧ ¬B).
Step 1: use De Morgan’s law to replace ¬ (A ∧ ¬B) with (¬A) ∨ (¬¬B).
Step 2: use the double complement rule to replace ¬¬B with B.
Step 3: the simplified expression is therefore (¬A) ∨ B.
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
1 Simplify the expression 0 ∨ (1 ∧ A).
Step 1: use the identity laws to simplify the contents of the brackets.
Step 2: decide whether the resulting expression can be simplified any further.
2 A logic circuit manufacturer has a limited stock of OR gates. Rewrite the
expression ¬ ((A ∨ A) ∨ B) so that no OR gates are used.
Step 1: use an appropriate law to simplify (A ∨ A).
Step 2: use De Morgan’s laws to rewrite the resulting expression without using ∨.
3 Simplify the expression B ∨ ¬ (A ∧ B).
Step 1: apply De Morgan’s laws to the expression where appropriate.
Step 2: another law can be used to simplify this even further. You may need to use the
associative laws to rearrange the order of the equation so that this becomes obvious.
Step 3: write down the new simplified version of the expression.
8 Boolean algebra 79
C Practice questions
8 Boolean algebra
Vector representation
A vector is an element that can be represented and implemented in a variety of ways, for
example, points in space, lists and mathematical functions.
Arrows
((
A vector can be written as A = ( r , θ ) or A = r . This vector has two
θ
y
12
dimensions: the first (r) is the size of it, and the second (θ) is the 11
10
direction of it. 9
8
In Figure 9.1 the arrow represents a vector. If we call this arrow 7
6
vector A, then the dimensions will be its position on the x and y axis. 5
4
The x-coordinate is 7, the y-coordinate is 10. This vector is written as 3
7 2
A = (7, 10) or A = . This can be extended into three-dimensions 1
10
0
with a z-axis, e.g. A = (2, 3, 4). 0 1 2 3 4 5 6 7 8 9 10 11 12 x
Figure 9.1
Data structure
A vector can be represented as an array or list. Arrays can be drawn as a table. An
example is shown in Table 9.1.
Index 0 1 2 3 4 5 6 7 8 9
Data 22 13 56 74 11 5 6 89 51 21
The index is the position of the data, starting traditionally at 0 (this may differ if you are
writing an array in a programming language, because some languages start counting at 0,
others at 1).
This type of vector can be written as a dictionary structure. Table 9.1 could be written as:
{0:22, 1:13, 2:56, 3:74, 4:11, 5:5, 6:6, 7:89, 8:51, 9:21}
Function
A vector can also be represented as a mathematical function, where an input is mapped
to an output in the format F : S → R where:
■ F represents the function.
■ S represents the set of values the function is applied to.
■ R is the output of the function.
9 Vectors 81
An example of a function would be f (x) = 10x where:
9 Vectors
■ f is the function.
■ x is the set of values the function is applied to.
■ 10x is the output of the function.
A Worked examples
a Draw a graph to represent the vector A = 2 .
3
Step 1: draw the axes.
y
Figure 9.2
Step 2: add the figures on the axes. The numbers on the horizontal axis need to go to at least 2, the
numbers on the vertical axis need to go to at least 3.
y
12
11
10
9
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 x
Figure 9.3
Step 3: draw the arrow representing the vector. It should start from (0, 0), with the head at (2, 3).
y
12
11
10
9
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 x
Figure 9.4
Table 9.2
Index 0 1 2 3
Data 9 14 2 3
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
10
1 Draw a graph to represent the vector A = .
1
Step 1: draw the axes.
Step 2: add the figures on the axes. The x-axis needs to go to at least 10, the y-axis
needs to go to at least 1.
Step 3: draw the arrow representing the vector, starting from (0, 0) with the head at
(10, 1).
2 Table 9.3 shows the elements in an array.
Table 9.3
Index 0 1 2 3
Data 4 3 2 1
C Practice questions
3
3 Draw a graph to represent the vector A = 12 .
2
4 Draw a graph to represent the vector A = 9 .
5 Table 9.4 shows the elements in an array.
Table 9.4
Index 0 1 2 3 4 5
Data 56 42 12 1 55 12
Table 9.5
Index 0 1 2 3 4 5 6 7
Data 7 6 5 4 3 2 1 0
9 Vectors 83
Vector addition
9 Vectors
Addition can take place on two (or more) vectors, when they have the same number of
dimensions.
The x-components are added, the y-components are added and any further components
(e.g. z) are added.
A Worked examples
2 5
a Add the two vectors A = and B = .
3 6
Step 1: add the x-components: 2 + 5 = 7.
Step 2: add the y-components: 3 + 6 = 9.
Step 3: write the new x- and y-components: A + B = 7
9
5 9 10
b Add the three vectors A = 10 , B= 5 and c = 2 .
6 4 1
Step 1: add the x-components: 5 + 9 + 10 = 24.
Step 2: add the y-components: 10 + 5 + 2 = 17.
Step 3: add the z-components: 6 + 4 + 1 = 11.
24
Step 4: write the new x-, y- and z-components: A + B + C = 17 .
11
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
10 3
1 Add the two vectors A = and B = .
2 4
9 Vectors
1 8
3 Add the two vectors A = and B = .
3 5
97
4 Add the two vectors A = 55 and B = .
12 12
12
5 Add the two vectors A = and B = 13 .
7 6
10 20 19
6 Add the three vectors A = 11 , B= 21 and C = 18 .
12 4 17
8 22 43
7 Add the three vectors A = 77 , B= 65 and C = 90 .
3 2 102
54 1 91
8 Add the three vectors A = 13 , B = 3 and C = 56 .
16 2 12
A Worked examples
4
a Multiply the vector A = by the scalar 3.
6
Step 1: multiply the x-component by 3: 4 × 3 = 12.
Step 2: multiply the y-component by 3: 6 × 3 = 18.
12
Step 3: write the new vector with a different letter: B = .
18
2
b Multiply the vector A = 3 by the scalar 5.
4
Step 1: multiply the x-component by 5: 2 × 5 = 10.
Step 2: multiply the y-component by 5: 3 × 5 = 15.
Step 3: multiply the z-component by 5: 4 × 5 = 20.
10
Step 4: write the new vector with a different letter: B = 15 .
20
9 Vectors 85
B Guided questions
9 Vectors
Copy out the workings and complete the answers on a separate piece of paper.
12
1 Multiply the vector A = by the scalar 10.
4
Step 1: multiply the x-component by 10.
Step 2: multiply the y-component by 10.
Step 3: write the new vector with a different letter.
100
2 Multiply the vector A = 101 by the scalar 2.
102
Step 1: multiply the x-component by 2.
Step 2: multiply the y-component by 2.
Step 3: multiply the z-component by 2.
Step 4: write the new vector with a different letter.
C Practice questions
3 Multiply the vector A = 9 by the scalar 4.
5
4 Multiply the vector A = 12 by the scalar 2.
3
5 Multiply the vector A = 9 by the scalar 5.
14
6
6 Multiply the vector A = 2 by the scalar 6.
10
5
7 Multiply the vector A = 7 by the scalar 3.
12
50
8 Multiply the vector 60 by the scalar 8.
70
9 Vectors
2 12
a Calculate the result of A . B where A = and B = .
4 4
Step 1: multiply the x-values together: 2 × 12 = 24.
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
5 2
1 Calculate the result of A . B where A = and B = .
6 3
Step 1: multiply the x-values together.
Step 2: multiply the y-values together.
Step 3: add the results together.
12 5
2 Calculate the result of A . B where A = 30 and B = 9 .
2 10
Step 1: multiply the x-values together.
Step 2: multiply the y-values together.
Step 3: multiply the z-values together.
Step 4: add the results together.
C Practice questions
3 Calculate the result of A . B where A = 1 and B = 5 .
3 12
4
Calculate the result of A . B where A = and B = .
19
4
4 23
5 Calculate the result of A . B where A = 2 and B = 71 .
1 65
8 12
6 Calculate the result of A . B where A = 6 and B = 3 .
2 4
9 Vectors 87
2 10
9 Vectors
7 Calculate the result of A . B where A = 3 and B = 11 .
4 12
5 6
8 Calculate the result of A . B where A = 2 and B = 10 .
2 20
9 Vectors
12
C = (10, 10). This arrow is outside the hull therefore 11
it is not a convex combination. 10
9
There is a formula that states what the vectors 8
7
must be multiplied by in order to gain a convex 6
combination. 5
4
D = αA + βB 3
2
A and B are the two vectors. α and β are the 1
0
numbers that the vectors will be multiplied by. 0 1 2 3 4 5 6 7 8 9 10 11 12 x
These must both be >0, and α + β = 1. Figure 9.8
A Worked examples
a A vector is represented by D
D = 0.2 × 12 + 0.8 × 2 .
3 8
12 2
Is D a convex combination of A = and B = ?
3 8
2 7
b Two vectors are represented as A = and B = . A combination of these is
4 10
D = 0.4 A + 0.8 B .
Is D a convex combination of A and B?
B Guided questions
Copy out the workings and complete the answers on a separate piece of paper.
12 2
1 Two vectors are represented as A = and B = . A combination of these is
3 1
D = – 0.13A + 0.95B.
9 Vectors 89
29 12
9 Vectors
2 Two vectors are represented as A = 100 and B = 6 . A combination of these is
20 9
D = 0.5A + 0.6B.
Is D a convex combination of A and B?
Step 1: check that α and β are both > 0.
Step 2: check that α + β = 1
If Step 1 and 2 are both True, D is a convex combination, otherwise it is not.
C Practice questions
7 12
3 Two vectors are represented as A = and B = . A combination of these is
8 3
D = 0.2A + 0.8B. Is D a convex combination of A and B?
100 99
4 Two vectors are represented as A = 101 and B = 100 . A combination of these is
102 101
D = – 0.123A + 0.99B. Is D a convex combination of A and B?
4 2
5 Two vectors are represented as A = 10 and B = 7 . A combination of these is
15 3
D = – 0.8A + 0.2B. Is D a convex combination of A and B?
13 5
6 Two vectors are represented as A = and B = . A combination of these is
14 4
Functions
A function is a mathematical concept that takes an input set of values (called the domain)
and maps them to elements in another set (called the co-domain). Each element of the
domain is related to exactly one (and only one) element of the co-domain. This means
that each input has exactly one output, but the same output can be produced from
multiple different inputs.
Domain Co-domain
f(x) = 2x 1
2
1
3
2
4
3
5
6
A very simple function would be one that multiplies the input value by 2, defined by
f(x) = 2x, shown in Figure 10.1. In this case, each element of the input set {1, 2, 3} would
be mapped to the elements of the output set {2, 4, 6}, which are all drawn from the
domain . This is a one-to-one mapping as each input element maps to a unique output
element.
However, suppose a function took an input string and worked out the number of ways the
letters in a word could be arranged. In this case, the input value ‘cat’ could be arranged
in 6 ways and so ‘cat’ maps to 6. However, the letters in ‘dog’ can also be arranged in 6
ways and so both ‘cat’ and ‘dog’ map to 6. This is known as a many-to-one function as
many input values can map to the same output value. A one-to-many function is not valid
as it allows the same input to produce multiple different outputs.
Types of functions
Functions can be divided into different types based on the calculation that is used to
transform the input value to the output value. Table 10.1 shows the types of functions that
are covered in the AS and A-level computer science specifications.
Functions in programming
Functions are commonly defined when creating computer programs using a high-level
language. For example, the mathematical function f(x) = 2x + 7 can be defined in Python
using the following code:
def myfunc(x) :
return (2*x) + 7
A programmer could then pass a value (such as 9) into the function by using the code
myfunc(9), which would return the value 25. This function would be a linear function.
A Worked examples
a For the function f(x) = 3x2 + x, give the set of output values if the input set = {5, 10, 20}.
Step 1: take the first input value (5).
Step 2: replace x in the equation 3x2 + x with 5, giving 3 × 52 + 5.
Step 3: calculate the output value, remembering that BIDMAS means that powers are calculated
first, then multiplications, then finally additions. In this case this gives 80.
Step 4: repeat the above three steps for the other values of the input set, giving {80, 310, 1220}.
b Define whether each of the following functions is linear, polynomial, exponential or logarithmic.
i f(x) = 5x5 + 7x2 + 9
Polynomial, as x5 and x2 both appear in the calculation.
ii f(x) = log10(x) – 9
Logarithmic, as the logarithm of x with base 10 is calculated.
iii f(x) = 5(x + 1)
Exponential, as the input value is used as the power to raise to (in this case, x + 1).
iv f(x) = 7x + 1
Linear, as it does not contain a polynomial, logarithm or exponent.
C Practice questions
4 A player of a card game chooses four cards and can play these cards in any order she
chooses. Calculate the number of permutations available.
5 For the following functions, give the set of output values if the input set = {2, 4, 6, 8}.
a f(x) = 2x – 1
b f(x) = 5x + x
c f(x) = x4 + x2
6 Define whether each of the following functions is linear, polynomial, exponential or
logarithmic.
a f(x) = 12 + log2(x)
b f(x) = 7x3 + 2x2
c f(x) = 2x + 18
def test(a) :
b = a * 3
if b > 10:
return (b – 10)
else:
return b
a Calculate the output from the function if test(2) is called in the program.
b Calculate the output from the function if test(7+3) is called in the program.
c Calculate the output from the function if test(7)+3 is called in the program.
Complexity of algorithms
Note: this topic is assessed at A-level only.
Every step in an algorithm takes a tiny amount of time for the processor to complete.
However, in complex algorithms, this time can add up to quite a significant amount. A
brute force algorithm to try every permutation of a 15-character password would take
over 500 years with a standard desktop computer.
It is useful to be able to compare algorithms to decide quite how complex they are.
Computer scientists use Big-O notation (pronounced ‘big oh’) to do just that. This
is called Big-O as it is written as O(x), where x is the worst case complexity of the
algorithm being analysed.
Big-O does not give us an answer for how quickly the algorithm will execute. Instead,
it gives an idea of how the execution time will increase as the size of the input data set
increases. An algorithm may run efficiently with a set of 10 input values, but how does
it scale up (that is, how quickly does it execute) with 10 000 or 1 million input values?
Big-O notation helps to answer this question.
Types of complexity
Algorithms with constant complexity are said to have complexity of O(1). This means
that, no matter how large or small the input set is, the algorithm always takes the same
amount of time to execute. These algorithms obviously scale perfectly!
Time to complete
Size of data
Time to complete
in the following examples, stands for the size of the
data set). A good example of this is a binary search,
which increases the number of steps to be executed
by 1 every time the data set doubles. A binary search
of 4 numbers may take t steps, but a binary search
of 2000 numbers can be completed in 10t steps and
Size of data
a search of 2 000 000 numbers can be completed in
20t steps. Algorithms with O(log n) efficiency scale Figure 10.3 Logarithmic
extremely well. complexity: time to complete
Time to complete
to add up all of the numbers in the input set will have
complexity O(n). A set of 5 numbers may take 5 steps,
but a set of 100 numbers will take 100 steps to execute.
Size of data
writing the program and wondering why it is taking so Figure 10.6 Exponential
long to run. complexity: time to complete
through the algorithm and looking at the complexity of each line or section. The beauty of
Big-O is that only the parts which have the most effect on how it scales up are included. The
types of complexity listed on the previous pages are arranged in order, with those with the
least effect at the top and those with the most effect towards the bottom.
For example, an algorithm may have three lines, one with complexity O(n), another with
complexity O(n2), and a third with complexity O(2n3). We can ignore the line with linear
O(n) complexity as this has less effect than the next two lines that both have polynomial
complexity. Of the next two lines, cubic complexity has more effect than quadratic
complexity. We can also ignore the multiplier of 2 in 2n3 as this has less effect than the
power. We can therefore say that this algorithm has O(n3) complexity.
A Worked examples
a An algorithm has an input data set of size n and takes 3n5 + 2n3 + 2n + 7 steps to run. Use Big-O
notation to state the complexity of the algorithm.
Step 1: look at each term and classify according to the type of complexity. 3n5 and 2n3 both have
polynomial complexity, 2n has exponential complexity and 7 has linear complexity.
Step 2: decide which of these has the highest complexity according to the notes. Here, this is the
exponential complexity.
Step 3: this algorithm therefore has O(2n) complexity.
b Use Big-O notation to give the complexity of the following algorithm where n is the size of the
input data set.
for x = 1 to n
for y = 1 to n
z = data[y]
print z
next
next
print ‘finished!’
Step 1: split the algorithm into sections: there is a nested loop with two instructions for each
repetition and then a print statement at the end.
Step 2: the two lines inside the nested loop run once when n = 1. The loop runs 4 times when n =
2 and 16 times when n = 3. This therefore grows with n2 complexity. As there are two instructions
inside the loop, this section takes 2n2 steps to execute.
Step 3: the final print statement is outside of the loop and so runs once no matter what the size of the
input. This statement has O(1) complexity.
Step 4: looking at the section with the highest complexity and simplifying, the whole algorithm has
complexity O(n2).
C Practice questions
4 An algorithm has an input data set of size n and takes 4n3 + 4n2 steps to run.
Use Big-O notation to state the complexity of the algorithm.
5 An algorithm has an input data set of size n and takes (log n) + 3 steps to run.
Use Big-O notation to state the complexity of the algorithm.
a data set of 100 items. Use Big-O notation to state the complexity of the algorithm.
7 An algorithm takes 6 steps to run with a data set of 1 item and 36 steps to run with
a data set of 6 items. Use Big-O notation to state the complexity of the algorithm.
8 Use Big-O notation to give the complexity of the following algorithm where n is the
size of the input data set.
if n > 10 :
for x = 1 to n
y = data[n]*7
print y
next
else
y = 7
print y
end if
1 a Convert the denary number 183 into an 8-bit binary number. (1)
b Convert the binary number 01101011 into denary. (1)
c Convert the denary number 153 into hexadecimal. (1)
d Convert the hexadecimal number F0 into denary. (1)
e State how many different numbers can be represented with 16 bits. (1)
2 a State how many kB are in 3 MB. (1)
b State how many bytes are in 1 GB. (1)
c Describe the difference between a MiB and a MB. (3)
3 State the result of each of these calculations.
a 10 DIV 2 (1)
b 100 MOD 3 (1)
c 2 MOD 5 (1)
d 50 DIV 50 (1)
4 IF x <= y THEN
OUTPUT x
ELSE
OUTPUT y
END IF
a State the output of this algorithm if x = 2 and y = 3. (1)
b State the output of this algorithm if x = 5 and y = 5. (1)
c State the output of this algorithm if x = 100 and y = 99. (1)
5 01 x = 0
02 DO
03 INPUT x
04 UNTIL x != 0
State the purpose of line 04. (1)
6 a Perform the binary addition 01100110 + 10000110. (2)
b i Perform the binary addition 10111010 + 11011001. (2)
ii Explain the error that would occur with your answer to part bi. (2)
7 a Perform the binary multiplication 0101 × 11. (3)
b Perform the binary multiplication 111011 × 110. (3)
c i Convert the denary numbers 30 and 3 into binary. (2)
ii Perform the binary multiplication between the two binary numbers from part ci. (3)
Exam-style questions 99
8 a Convert the denary number –50 into an 8-bit sign and magnitude number. (1)
Exam-style questions
b Convert the denary number 100 into an 8-bit sign and magnitude number. (1)
c Convert the denary number 329 into a sign and magnitude number. (1)
d Convert the denary number –444 into a sign and magnitude number. (1)
9 a Convert the sign and magnitude number 01101101 into denary. (1)
b Convert the sign and magnitude number 10101110 into denary. (1)
c Convert the sign and magnitude number 010110110110 into denary. (1)
d Convert the sign and magnitude number 111011011010 into denary. (1)
10 a Convert the denary number –42 into an 8-bit two’s complement number. (1)
b Convert the denary number 75 into an 8-bit two’s complement number. (1)
c Convert the denary number 299 into a two’s complement number. (1)
d Convert the denary number –501 into a two’s complement number. (1)
11 a Convert the two’s complement number 01011000 into denary. (1)
b Convert the two’s complement number 11111100 into denary. (1)
c Convert the two’s complement number 010001111001 into denary. (1)
d Convert the two’s complement number 100011101011 into denary. (1)
12 a Perform the binary subtraction 25 – 5. (3)
b Perform the binary subtraction 100 – 50. (3)
c Perform the binary subtraction 294 – 63. (3)
d Perform the binary subtraction 1000 – 992. (3)
13 a Convert the unsigned fixed point number 01011.101 to denary. (2)
b Convert the unsigned fixed point number 11011011.0101 to denary. (2)
c Convert the two’s complement fixed point number 101011.0101 to denary. (3)
d Convert the two’s complement fixed point number 0101.10101 to denary. (2)
14 a Convert the denary number 23.375 into unsigned fixed point. (2)
b Convert the denary number 100.75 into unsigned fixed point. (2)
c Convert the denary number 20.34375 into two’s complement fixed point. (2)
d Convert the denary number –30.34375 into two’s complement fixed point. (2)
15 a i Perform a 2-place right shift on the 8-bit binary number 10010110. (1)
ii Explain the impact of the shift performed in part ai on the binary number. (2)
b i Perform a 3-place left shift on the 8-bit binary number 10110111. (1)
ii Explain the impact of the shift performed in part bi on the binary number. (2)
16 a Perform a bitwise OR operation on 10101101 using the mask 11111111. (1)
b Perform a bitwise AND operation on 10101111 using the mask 10101010. (1)
c Perform a bitwise XOR operation on 00110110 using the mask 11011111. (1)
d Perform a bitwise NOT operation on 10111011. (1)
17 Two vectors A and B are stored as A = (12, 15) and B = (25, 55).
a Calculate the result of adding the vectors A and B together. (2)
b Calculate the result of a dot product of these two vectors. (1)
c i Multiply vector A by the scalar 5. (1)
ii Multiply vector B by the scalar 15. (1)
Exam-style questions
a 180 – 15 × (6 + 7) (1)
b 15.73 (1)
c 9 (1)
d 10 (1)
19 If A = {10, 20, 30, 40, 50, 60, 70} and B = {5, 10, 15, 20, 25, 30} find:
a A∩B (1)
b {4x + 1 | x ∈ A } (1)
20 Find A where A = {x | x ∈ ∧ x >10 ∧ x < 20}. (1)
21 Calculate the denary values of the following floating point binary numbers. All
values are given in two’s complement and use 4 bits for the exponent and 4 bits for
the mantissa.
a 01001110 (1)
b 01100011 (1)
c 10100100 (1)
22 State whether each of the floating point binary numbers below is normalised or not.
a 010010101 (1)
b 000101101 (1)
c 101001010 (1)
d 110010100 (1)
23 Calculate the largest number that can be stored using a 10-bit binary number if
the number uses 6 bits for the exponent and 4 bits for the mantissa. (4)
24 Calculate the absolute and relative errors when:
a 256.72 is stored as 256 (2)
b 0.7 is stored as 1 (2)
c 947 is stored as 900 (2)
25 A bitmap image with resolution 256 × 1024 uses 136 unique colours. Calculate
the file size of the image in kilobytes. (4)
26 A 1-minute sound file is recorded using a sample resolution of 16 bits and a
sample rate of 44 kHz. Calculate the file size in megabytes, giving your answer
to 3 decimal places. (2)
27 Complete the truth table for the expression A AND B OR (NOT C). (8)
Table E.1
A B C A AND B OR (NOT C)
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Specification cross–reference
The information in the table below is intended as a guide only. You should refer to your
specification for full details of the topics you need to know.
Appendix 103
Representing 1.4.1 1.4.1 3.5.4.3 4.5.4.3
Appendix
negative
numbers:
Subtracting
with two’s
complement