CAMBRIDGE
UNIVERSITY PRESS
Computer Science
for Cambridge International AS & A Level
COURSEBOOK
Sylvia Langfield & Dave DuddellCAMBRIDGE
Lary Ping Me Cambiege 288 Unt gon
oe Ube Pia, 20 oe New e005 USA
1 Vilamstonn Rd Por Malou C2, Raala
234-21, 34 on, Pat Splendor Frum J Dai Cente
"anon os 5-4, gape OT5N5
amie Unverty Prep a e uoesty of Cog
farthest ton by dasareaing now eps!
‘Ehtaton tring dere thehighesnaacora eso eee.
camer
Vnomatonontirtieswnecorbydgeoy/SBLGSSSES
ecorbedgeuriestyPess2018
Thepubtston econ Suber sts cepon
sot rnsansaleeet coche ering ere,
eprasicnnol any pu mey hace went mien
pemicon fants ues res
Gress eon 20:8
Pein thei ingot Latina rnd
Aectloue rc ri ebcton kel fo he Bea
{SBN 971-108-5832 Paperback nth Cambridge Eleste ction (yes)
Isa sre tion eons cana Semen yrs
amt Uniesiy reshapes te psn or acum of URL
{Grr rts re sees lero mhptteaton, ad casa
‘Renton eases ot reas anda tal lmao enh
‘Soworiscrecate rea esramnngbt sme Urey soos ot
[fone e esac oaveneoraton eter
ieomoleqosion adsorlestvesinthe be wee witenby ents
fisamaataa he waymatsse ardcomay bere
Pst ecmpaper queso protic permission of Canbige
‘Sousa ctsnanona Evo
once 0 TEACHERS THE UK
lesitesreredce ary oar. wort wt em feng
Soteyenans eco tongs oat caer helene:
alec chargers eon
Cooyiheucenne tere
(i sherenossctcohn tt orwheryouisn wend etemscfs eres,
‘Sedyeuhoe pede wien meson olay Users es
( wheeyoc se plowedoeplodsrsthoupemauon ude Berens
‘eam 3th Capra bears and Patent 88 ch cove
‘arate fe epecotor ceva pesngerwincenan pes ect
Strona mpoducton lots rps sting aaron gsi(eT)
Part 1: Theory fundamentals
Part 2: Fundamental problem-solving and programming skills
pter 12 Algorithm des m-soving F
Part 3: Advanced theory
Part 4: Further problem-solving and programming skillsCeCe a1)
Part 1: Theory fundamentals
Part 2: Fundamental problem-solving and programming skills
Part 3: Advanced theory
Part 4: Further problem-solving and programming skillsHow to use this book
ey Terns
Task.
uestion -
Discussion Point ~
Rellection Point-
Reflection PontCa
Extension Question - oxerdee
adv
or topics beyond the immediate scope ofthe
‘ge Internationa AS. ALevel sylabus.
On Extension Question 101
Worked Example
3 of song problems orimplementing
Tip quik notee'o
Dussieinseen bis hihie 12 wheres ean oid vales po hich
ery
summary =hese
‘appear aithe end of
youre
have learned
an Exam-style Questions
‘cxamstyle Questions -
‘sing exam style questioneaChapter 1:
Information representation
By theend ofthis chapter you should be abet:
1 sha understanding of binary magnitudes andthe
derence between binary preies and decal prefs
1 show undertanangof thebasisof ferent ruber systens
‘= perorm binary adtion and subtraction
‘dese practical applications where BnaryCoded
Decimal (20) andHexadecima re used
1 show understandirgofand be ableto represent
characte ta nits intemal binary form, depending on
the character set used
1 show understanding ofhow data for abitmapped image
1 perform calevlationsto estimate the fiesizetora bitmap
Image
show understandiag of the effects of changrg elements
ofa bitmap imageon telmage qulty ar
show understanding ofhow dat fora vector gaphicare
justi the use oa itmap image ora vector rphicfora
ven task
‘= show undersandingothow sounds represented and enced
‘show understanding ofthe impact of changing the sampling
rate and elution
show understanding ofthe need for and examles ofthe use
cofcompresion
show understanding of oss and lssess compression and
ust the use of a method ina give stuation
show understanding of how a tet le, bitmapimage, vector
\gephicand sound fie canbe compresadReece Eee)
1.01 Number systems
Denary numbers
Asacildwe ist encounter the numbers that we use in everyday ie when We ate fist
learsing to count Specialy, we lam to count using 2,3, 5.6.7.8, 9,10 This givesus
ten ciferent symbols to represent each indvidual cig. This stereloe a base-10rumber
system. Numbers inthis sytem are caled denary aumbersc, re traditional, deca)
numbers,
‘When 2 numbers wntten down the value thatitreresents is defined by the place values of
‘the gets nthe numer. This canbe ilustated by considering the denary number 345 which
is interpreted as shown in Table 1.01
Piacevalie wow [wn]
Gomer eee
0 oe |e
Tobie 1 Ue of placevalesn the representation denry uber
Binary numbers
‘The snarynumbersystem sbase 2 Each binary itis writen with ether ofthe symbols
and i, Abinary digits eared to as Bit
swith a denary number, th zlueo a binary aumoeris defined by placevales. For
‘example, see Table 102 forthe inary number 013.0,
Produc of digit and .
place value
“Table 1.02 Use of place valuesin the representation of binary number
8y addingup the valuesin the bottom row you can see thatthe binary number 101110 has 2
velue whichis equivalent the denary mmbers6
‘You mustbe able to use the binary number system in order to understand computer,
systems, Tis i because nse computer systems thee no attempt made to reprdiferent igs
¥. nsteao all computer technology is engineered with components
lepreset or recagrise ony two states: on'and'off To match this, allsoftware used
by te hardware uses binary codes which consist bits. The binary code may represent
binary number but this es nat have tobe the case
Binary codes are mostoten based onthe use ofone or more groups of eight bits. Agroup of
bitsiscaleda bate
Hexadecimal numbers
These ae base-16 numters where each hexadecimal gts represented by o
folowing symbols 0,,234.567.89,48,0.EF, The symbols through to represent
the denary values 10 trough to 15. The value ofa numbers defined by place voles. For
trample. 200 lable 1Usfor tie nexacecimal number 2A,
or
Perens
Table 1.03 Us of place vluesin the representation ofa hexadecimal number
[Adc up the valuesin the bottom row shows that the equivalent denarynuberis 78
Inorderto explain why hexadecimal nrmbers are us
groupe fours
enced fisttodefnethe mibbleasa
_Anibbe can be represented ty one hexadecimal cgi Ths means that each byte of binary
code can be written 3s we hexadecimal digits. Two examples are showin Table 104
together with thei denary equivalent
a
200100 a8 0
‘able 1.08 Examples of byte represented by two lheradecimal gis
Note here that you were converting the binary number
exercise on 2 piece of pane you would not bother wit nluting leading zeros. However, 2
Slolowed trough in the hexadecimal epresetation
‘ne example when you wil see hexadecimal representations of bytes is hen an error has
‘eeutred dung the execution ofa program. Amemary dump could be provided whichhas 3
ovalec al fepesentaton ofthe content of some chosen part the memory. Another use
‘when the bytes conan binary numbers inthe charts that define character codes. This
dscussed iaterin this chapterIne character code chars and im other onlin sources you may se eterencesto octal
rumbers which are base-8 You can ignore these
Converting between binary and denary numbers
‘One methee for converting binary number toa denary number's toad upthe place
values or every git that has a value 1. This wasilustrate in Table 1.02,
‘An alternative metho is shown Worked Example 1.0
Tocarryoutthe conversion you stat atthe mast sigficant bt and sucessvly multiply bytwo and ad the result the
ext cit. The folowing shows the method being used to convert the binary number 1100 othe denary number 25:
2442101, then
add6100, then
3d 12100,then
3d 2410 1to gives
Toconvert a denary number to binary begin by denying the largest power of thathas a
vale less than the denary number. You can then write doum the inary representation ofthis,
ome of2 value. This wile 2 1 folowes byte approprate umbe-of eros.
Now subtract the power oftwo value From the denary number. Thendentythe largest
power of2 vale that ess than the remainder rom the subtraction. You can now repace a
zeroin thebinary representation with aL fr this new poner of2 postion
Repeat tis process unt you have accounted forthe ful deny nue,
For example, forthe denary umber 7 thelarget power of two vale less than hiss 64 so
you can start by witing down 1000000, The remainder ater subwracing 64 fam 78s 16 The
largest poner cf two valueless than thisis 80 the replacement of zero by J gives 100100,
Repeating he procezstinds values of4 then 220 the nal answe 00114.
‘An akernative approachis shown in Wocked Example 1.02
‘useful way to convert a denary valve tots binary equivalent isthe aracedut af secosive clusion by twowith the
‘emeinder written down at ech tage, The converted number sthen even asthe se of remainders in reverse order,
‘This can beillustrated by the conversion of denary 246 to binary
26 = 2 > 123 withremaindero
23+ 2 > 61 withremaindert
30 withremalnder1
18 withremaindero
with mainder
with remainder 1
with remainder 1
» wit remainder 1
the binary equivalent of denary 2465 11120310.SE
a )
“Tochec tht anansne-wih eight it sensi, remember hatte maximum dear value
| ppbensemat shoe ionenntehnntaamtnartes an
fez,
Conversions for hexadecimal numbers.
Itispossibleto convert hexadecimal number to denary by using the method shown in Table
103, However, here aremore than 2 few cigs, the nurbersinvolved inthe conversion
become very lage nstead, the sensible approach iso fis convert the hexadecimal number
toa binary number which cen then be converted to denary.
Toconert hexadecimal numberto binary, each dg strested separately and converted into
a bitbinaryequvalent,emembering tat Fconvetsto 111, € converts to 1110 ands0 on
Toconvest a binary umber to hexadecimal you start withthe fou east ig
and convert nem ro one hexadecyl tox en proceed up
cont bits
Senificent bt successively aking groupings of fur bits and converting each grouping tothe
covresponding heradecimal it
TasKL.01
Convert each atthe denary umbers¥6, 218 and 374 nto hexadecimal numbers.
Wa eee
Questions.01
1,02 Numbers and quantities
‘There are several diferenttypes of numbers within the d
provided Tale 10,
5 system, Examples ofthese are
on [pest fe computers
“able 1.05 biferent nays to expressa vlueusingthe denaryaumber system
Weil focus on how large values ae cope
of measurement, canbe wtten in tre
te, fe havea quantity tat incldes uits
diferent ways. For exam
wien in any one af these ree ways:neces
Thesecond example has used an exponential notation to define the magnitude othe value
Te tid examplehas aéded a preixtthe unit ta define this magnitude We eed this as
25367klometres
The'klo'is an example of adecimal prefix There oe our cecimal pein commonly used
foslargeeurbes. These are shown in Table 106
cee
Uniortunately fora longtime the computing word used these prefixramesbutitha
slghly diferent dfinison. The valve for 2 102, Because ths is close to 1000 computer
Scietsts decides that they could use the kilo preixta represent 1028 So,forexsmple. ita
computer system had the fllowing valves quoted forthe processor speed and te sizeof the
‘memory and ofthe hard ce
Processor speed Lech
Se of RAM ace,
Ske ofhard disk 0068
The peti would represent 1° forthe processor speed but would slmoct cota
represen 1024 1028 1026 forthe ther to valves,
‘Ths unsatisfactory stuationhas now been esoved by the definition of anew tt of names
nich canbe used to deine abinary prefix, election ofthese is shown in Tate 1.07,
EIN Factor applied tothe value
ie 1
San gil aa
i ot
‘Table 1.07 Some examples ofbinary prefixes
When a number ora quant s presente tora person to ead itis best presented with
ether one enary cgi ortwo denary cigs before he decimal pint. fa caleulation has
been carted out, the nia result found may not match this equitement, Aconvetson ofCambridge international AS & A Level Computer Science
te presented valve wil be needed by choosing a sensible mapritude factor For example,
Consider the lowing wo answers calelated forthe sie of ale:
a 3ssobytes|
‘onerson tokibtytes would be sensible using the calubtion
AiO = 3275KB
bb s4sso00byte
a15608=
Here, conversion to mebibytes would be sensible using the caleltion
2 Iyig=3 296m
oe M32
34560008.
Ifa caluationisto be performed with values quoted with erent ragrtude actors there
mustfist be conversions to ensure all
jeshave the same magnitude factor For example,
many files of sie 2-4 MiB could be storedon a 4GiB memory stick
rversion ofthe GiB valueta the corresponding Ni value
ifyou needed tonow
there should bes
The aleulaion would be
xa024t8
1.03 Internal coding of numbers
The discussion inthis chapter lates only to the codngot integer values. The codingot non
integer numeicvalues (el number) isconsieredin Chapter 6 (Setion 16.03,
Coding forintegers
omputersneed to