55
Computer Science A Level – Bridging Unit Introduction................................................................3 C# Programming..............................................................4 Getting Started........................................................... 4 Data Types & Variables.................................................... 4 Variable Names............................................................ 5 Constants................................................................. 6 Comparison Operators...................................................... 6 Arithmetic Operators...................................................... 7 Programming Tasks A....................................................... 8 Selection................................................................. 9 Programming Tasks B...................................................... 12 Count-Controlled Iteration............................................... 13 Programming Tasks C...................................................... 14 Condition-Controlled Iteration........................................... 16 Programming Tasks D...................................................... 18 Extra Reading............................................................ 18 Data Representation........................................................19 Pure Binary.............................................................. 19 Binary Question A........................................................ 20 Binary Addition.......................................................... 21 Binary Question B........................................................ 21 Two’s Complement......................................................... 22 Binary Question B........................................................ 22 Binary Subtraction....................................................... 22 Binary Question C........................................................ 23 Binary Multiplication.................................................... 23 Binary Question D........................................................ 23 Fixed Point Binary Fractions............................................. 24 Page | 1

Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Computer Science A Level – Bridging Unit

Introduction................................................................................................................................................................3

C# Programming..........................................................................................................................................................4

Getting Started........................................................................................................................................................4

Data Types & Variables............................................................................................................................................4

Variable Names.......................................................................................................................................................5

Constants................................................................................................................................................................6

Comparison Operators............................................................................................................................................6

Arithmetic Operators..............................................................................................................................................7

Programming Tasks A..............................................................................................................................................8

Selection..................................................................................................................................................................9

Programming Tasks B............................................................................................................................................12

Count-Controlled Iteration....................................................................................................................................13

Programming Tasks C............................................................................................................................................14

Condition-Controlled Iteration..............................................................................................................................16

Programming Tasks D............................................................................................................................................18

Extra Reading........................................................................................................................................................18

Data Representation.................................................................................................................................................19

Pure Binary............................................................................................................................................................19

Binary Question A..................................................................................................................................................20

Binary Addition......................................................................................................................................................21

Binary Question B..................................................................................................................................................21

Two’s Complement...............................................................................................................................................22

Binary Question B..................................................................................................................................................22

Binary Subtraction.................................................................................................................................................22

Binary Question C..................................................................................................................................................23

Binary Multiplication.............................................................................................................................................23

Binary Question D..................................................................................................................................................23

Fixed Point Binary Fractions..................................................................................................................................24

Binary Question E..................................................................................................................................................24

Hexadecimal..........................................................................................................................................................25

Hexadecimal Question A.......................................................................................................................................26

Page | 1

Page 2: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Extra Reading........................................................................................................................................................27

Digital Logic...............................................................................................................................................................28

Logic Gates............................................................................................................................................................28

Flip Flop.................................................................................................................................................................31

Half Adder.............................................................................................................................................................32

Full Adder..............................................................................................................................................................32

2 Bit Adder.............................................................................................................................................................33

Logic Gates Questions A........................................................................................................................................34

Further Reading & Activity....................................................................................................................................35

Logic Problems..........................................................................................................................................................36

Problem Solving.....................................................................................................................................................36

Logic Puzzles Tasks................................................................................................................................................36

More Puzzles.........................................................................................................................................................37

Thinking Like A Computer Scientist...........................................................................................................................38

Sudoku...................................................................................................................................................................38

Twisty Puzzles........................................................................................................................................................38

Electronics & Microcontrollers..............................................................................................................................38

Write Programs.....................................................................................................................................................38

Page | 2

Page 3: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

IntroductionThe aim of this document and these tasks is to help you prepare for the A level. There is a mixture of reading, practical exercises and written questions to help you to consolidate and extend the knowledge you have gained at GCSE.

Well over half of the A level is concerned with programming and programming techniques. That is the most important part of this document and the part you should concentrate on completing. Complete as many of the tasks as you can. The other sections are optional but will help you to make a flying start on the course.

Our main programming language for the A level is C#. Most of you will have already learned how to program to a reasonable standard in another language like Python or Visual Basic. The learning journey is far shorter when you start your next language. Don’t worry if you find these tasks hard at first. Keep plugging away and trust yourself to get there in time.

In order to program in C#, you will need to install Visual Studio Community Edition. This is free to use and is packed with features. If you are using a Mac or have a Linux operating system, you can install .NET Core or look for the Mono Project. Both are free to install but do have some minor differences from the code shown in this document. You should be able to adjust enough to get what you need from these activities.

Take the trouble to store all of your programs with sensible names. It will help you later to use them as a reference.

There is a lot of code in this document. If you hide spelling errors, it will be easier to read. There is a block of explanation to start with followed by some tasks to complete. You can, of course, do more programming for each section or overall than is included here. Some of the explanations and tables are copied from OCR’s programming reference for C#.

Page | 3

Page 4: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

C# ProgrammingGetting StartedFor most of your work, you need to write console programs. You will learn how to write applications with a GUI during the course but the bulk of the work involves writing for the console.

When you choose to make a new console program, you will see some generated code, something like,

Unless you are creating a subroutine or declaring a global variable, you write your code where the red box is.

Data Types & VariablesIn C#, variables have a fixed data type. In languages like Python, you can use a variable without first declaring it and can choose to store different types of data with it after you first define it.

Neither of those things are possible in C#. Before you can use a variable, you must declare it and you must decide the data type to use. You have a lot of choice.

Data Type

Range

byte 0 .. 255sbyte -128 .. 127short -32,768 .. 32,767ushort 0 .. 65,535int -2,147,483,648 .. 2,147,483,647uint 0 .. 4,294,967,295long -9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807ulong 0 .. 18,446,744,073,709,551,615float -3.402823e38 .. 3.402823e38double -1.79769313486232e308 .. 1.79769313486232e308decimal -79228162514264337593543950335 .. 79228162514264337593543950335char A Unicode character.string A string of Unicode characters. 2 bytes per character.bool True or False.

Page | 4

Page 5: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Variable NamesThere are some basic rules with variable names in C#:

• the first letter of a variable must be either a letter, underscore (_) or the @ symbol.• after this, they can be any combination of letters, underscores or characters• they can only be one word• they can only use letters, numbers and underscores (_)• hyphens are not allowed (-)• spaces are not allowed• they can’t begin with a number• special characters are not allowed such as $ or ‘.

Remember:

• variable names are case sensitive, SPAM and spam are different variables• it is convention to use a lower case letter at the start of a variable name• you can use camelCase or not_camel_case• a good variable name describes the data it contains• the variable type always comes before the variable name in C#.

In the following, notice how the data type comes first.

int parrotAge;

const string parrotStatus = "Alive";

C# requires you to declare the variable type at the same time as the variable name.However it does not require you to assign a value to the variable straight away.We can use the keyword const to ensure that parrotStatus cannot be changed during runtime

int parrotAge;string parrotStatus = "Alive";

parrotAge = 12;

Console.WriteLine("The parrot is currently " + parrotAge + " and is " + parrotStatus);

Once assigned you can use the variable with other values or variables as shown.Note that if you leave the variable ‘null’ when you declare it, you must assign a value to the variable first before being able to carry out further operations on it.

parrotAge = parrotAge + 1;Console.WriteLine(parrotAge);

A variable can be overwritten with a new value at any time.

You cannot assign data to a variable of different types. Each variable will only hold the data type defined.As you can see – an error is shown in the IDE as "two" is a string, and we are trying to assign it to an ‘int’.

ConstantsConstants don’t change after they have been defined.Page | 5

Page 6: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

const int ParrotAge = 0; The keyword for declaring a constant is const. It is used BEFORE the data type and declaration of the variable.If you try to use the keyword const and do not declare a value to the variable, then it will throw an error! As you can see – there is a warning line at the semi colon.Remember a constant cannot have a new value assigned to it during run time.

The purpose of a using a constant is to provide a word to use in place of a value which you might change before running the program or which is hard to remember.

C# programs are compiled before execution. When a program using constants is compiled, every time the constant appears, the compiler replaces it with the value of that constant.

Variables have a cost in memory usage, constants do not.

Comparison OperatorsIf you are a Python programmer, these are not new to you. Visual Basic programmers need to get used to the ==. This is to ensure that comparison and assignment operators are different in the language.

== Equal to!= Not equal to< Less than<= Less than or equal to> Greater than>= Greater than or equal to

When using Logical Operators, the answer to a comparison is always TRUE or FALSE (i.e. a Boolean result. Have a look at what happens with the following code, and results:

int valueA = 23;int valueB = 15;Console.WriteLine(valueA == valueB);Console.WriteLine(valueA != valueB);Console.WriteLine(valueA < valueB);Console.WriteLine(valueA <= valueB);Console.WriteLine(valueA > valueB);Console.WriteLine(valueA >= valueB);

You can also save these results as a variable.

Page | 6

Page 7: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

int valueA = 23;int valueB = 15;

bool myResult = false;myResult = valueA != valueB;Console.WriteLine("My Result = " + myResult);

Arithmetic Operators

+ Addition e.g. x=6+5 gives 11- Subtraction e.g. x=6-5 gives 1* Multiplication e.g. x=12*2 gives 24/ Division e.g. x=12/2 gives 6

NB Using integers will result in DIV being applied% Modulus e.g. 12MOD5 gives 2

Math.Pow(A, b); Exponentiation e.g. Math.Pow(3, 4) gives 81Note: both numbers need to be double for this to work.

Examples of Arithmetic operators:

int valueA = 23;int valueB = 15;

Console.WriteLine(valueA + valueB);Console.WriteLine(valueA - valueB);Console.WriteLine(valueA * valueB);Console.WriteLine(valueA / valueB);Console.WriteLine(valueA % valueB);

double result, number1, number2;

number1 = 2;number2 = 2;

result = Math.Pow(number1, number2);

Console.WriteLine(number1 + " ^ " + number2 + " = " + result);

C# does not have an operator or keyword for integer division. In Python, you use a double slash (floor division), Visual Basic does a backslash. In C#, using an integer data type means that you will get results that are integers, with the rounding going down.

Page | 7

Page 8: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Programming Tasks AType up the following example program. Notice where it fits in the generated code. Watch the screen as you type, the IDE (integrated development environment) is helpful to you when you program.

Press F5 to compile and run the program. Things to notice include,

• How the variables are declared.• How the prompt is created using Console.Write (no line break at the end).• How the input is received. Input at the console is always text. It needs to be converted. C# has stricter data

type control than other languages and will not do the work for you as Visual Basic would.• How the output of the result is done.

1. Change the program so that it

• subtracts the second number from the first• multiplies the numbers together• divides the first number by the second

2. Write a program to perform a conversion between Celsius and Farenheit.

3. Program a system which takes as inputs,

• The length of the base of a triangle. • The perpendicular height of the triangle.

The system will output the area of the triangle.

4. Program a system which takes as inputs,

• The average speed of a car over the length of a journey. • The distance that the car has to travel.

The system will output in minutes the length of time that the journey will take.

Page | 8

Page 9: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

5. Program a system that takes the three inputs required to calculate the area of a trapezium and outputs the area.

SelectionThere are two commonly used selection constructs in C#. There is an IF statement and a SWITCH statement.

You can also use the following logical operators,

AND: && OR: ||

With an IF statement, the condition is enclosed within brackets. The semi-colon is not used on the line with the condition but is used with the statements inside the curly braces.int duckWeight = 15;int personWeight = 13;

if (duckWeight >= personWeight){ Console.WriteLine("Clearly not a witch!");}

else { Console.WriteLine("She's a Witch!");}

Here the weight of the person is not greater than that of the duck, and therefore the IF statement executes, and the ELSE is skipped.

int duckWeight = 15;int personWeight = 34;

if (duckWeight >= personWeight){ Console.WriteLine("Clearly not a witch!");}

else { Console.WriteLine("She's a Witch!");}

Here the weight of the person is greater than the duck. This means that the IF statement is FALSE and does not run. Therefore, the ELSE statement executes by default.

Page | 9

Page 10: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Using else if…

int duckWeight = 15;int personWeight = 15;

if (duckWeight > personWeight){ Console.WriteLine("Clearly not a witch!");}

else if (personWeight > duckWeight){ Console.WriteLine("She's a Witch!");}

else{ Console.WriteLine("They weigh the same!");}

Note the subtle change in logic in the first IF statement with the removal of the ‘=’. Now the first two tests are for greater than. As the weights are the same, it checks both IF statements, which return false, and therefore executes the ELSE statement.

Using logical operators…

string caster = "witch";string victim = "peasant";bool later = true;string spellCast = "She turned me into a newt";string victimStatus = "I got better!";

if ((caster == "witch") && (victim == "peasant")){ Console.WriteLine(spellCast);}

- Both conditions here evaluate to true- Therefore the IF statement condition evaluates to true- Thus the IF statement is executed

string caster = "witch";string victim = "peasant";bool later = true;string spellCast = "She turned me into a newt";string victimStatus = "I got better!";

if ((caster == "witch") && (victim == "dog")){ Console.WriteLine(spellCast);}

Here the IF statement will not execute because the victim is not a dog.

BOTH conditions must be true for the IF statement to execute.

Page | 10

Page 11: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

string caster = "witch";string victim = "peasant";bool later = true;bool lie = true;string spellCast = "She turned me into a newt";string vicitmLies = "We believe you are lying!";string victimStatus = "I got better!";

if ((caster != "witch") || (lie == true)){ Console.WriteLine(vicitmLies);}

-At least one condition is true-Therefore the IF statement condition

evaluates to true-Thus the IF statement is executed

Nesting if statements inside each other,

if (Case A is true){ #This code will execute if (Case B is true) { #This nested IF code will execute }}else{ Console.WriteLine("They weigh the same!");}

Note the change in logic.

If Case A is false, the ELSE statement will execute.

If Case A is true then any code in that IF statement will run.

This will check the NESTED if statement.

Case B will ONLY be checked if Case A is true.

Case B IF statement code will ONLY execute if both Case A and Case B are true.

Page | 11

Page 12: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Programming Tasks B1. Write a program that reads two numbers and examines the relationship between them. Output one of the

following messages,

• A is greater than B• B is greater than A• A and B are equal

2. Write a program to find the two roots of the quadratic equation ax2 + bx + c = 0 using the formula,

Use the double data type to store a, b and c and the two values of x. The following pseudocode should help.

discriminant ← b * b – 4 * a * cIF discriminant less than 0 THEN

The equation cannot be solved with the formula.ELSE IF discriminant EQUAL to 0 THEN

There is a single solution to the equation, x=−b2a .

ELSE

There are two solutions, , x=−b+√b2−4 ac2a

and x=−b−√b2−4ac2a

.

END IF

3. Look at the following page on the Switch statement, http://multiwingspan.co.uk/cs.php?page=switch. Consider the circumstances in which it might be preferable to use a switch statement instead of an IF.

4. There is a third way to do selection. It is called the ternary operator. It is a very compact way to write conditional code. Have a look online to find out how to use it.

Page | 12

Page 13: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Count-Controlled IterationC# has a FOR statement. At first glance it appears a little confusing. It is still, in my view, preferable to the range statement in Python.

Couting forwards…

for (int count = 1; i <= 5; i++){ Console.WriteLine(i);}

Here is an example of the FOR loop in action. Our count is defined within the loop itself. We always know this loop will start a 1 and then iterate until the count = 5. When the count ‘i’ reaches 6, the loop will exit as the condition becomes false.

int countTo = 0;

Console.Write("What number do you want to count to?: ");

countTo = int.Parse(Console.ReadLine());

for (int i = 1; i <= countTo; i++){ Console.WriteLine(i);}

Here we are using a variable within the condition to vary the number of iterations.This means the loop has more functionality:Run 1:

Run 2:

However, what would happen if the user entered ‘0’ for a number to count to?

Page | 13

Page 14: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Counting Down…

int countTo = 0;

Console.Write("What number do you want to count from?: ");

countFrom = int.Parse(Console.ReadLine());

for (int i = countFrom; i >= 0; i--){ Console.WriteLine(i);}

You can also decrease your counter to reach a certain value.

The for loop has the following format,

for (a;b;c){

Statements to repeat}

• The a part of the statement is an assignment statement. It is usually used to assign a starting value for the counter.

• The b part of the statement is a condition. The condition is checked at the start of each iteration to determine whether or not that iteration should take place.

• The c part of the statement is an assignment statement. It is usually used to increase or decrease the value of the counter. In the examples, you see shorthand being used. When you say ++ after a variable, you mean, ‘add one to it’. You can choose to change the counter by a different amount or perform a different operation.

Programming Tasks C

1. Messages

Write a program that prompts the user for a message and a number. Your program should use a for loop to output the message the number of times that the user requests.

2. Tables

Write a program that prompts the user for a number from 1 to 12. The full multiplication tables should be displayed for that number.

For example,

User enters 5.

5 x 1 = 5 5 x 2 = 10

Page | 14

Page 15: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Your task here includes a specific format for the output. You must attempt to output the information as described.

3. Sum Of Cubes

Write a program that asks the user to enter a number. Use loops in your code to work out the sum of the cubes of all of the numbers from 1 to that number.

Sum Of Cubes 3: 1 + 8 + 27 = 36

4. Fizz Buzz

Write a program which iterates the integers from 1 to 50. For multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".

5. Fizz Buzz Sum

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

6. Special Pythagorean Triplet

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

a2+b2=c2

For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.

Find the product abc.

This is a tricky task. You will need to nest a loop inside a loop. You need a loop to explore all possible values of a, and all of the possible values of b. Since c can be worked out if you know a and b, you won’t need 3 loops.

Page | 15

Page 16: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Condition-Controlled IterationA for loop can be said to be an example of count-controlled iteration because the number of iterations depends on counting from a starting number to a target number. In programs, we often want to repeat sections of code until a condition is met or whilst a specific condition holds. This is called condition-controlled iteration.

This type of iteration comes in two flavours, pre-tested and post-tested. The two terms are used to describe whether the condition is examined before each iteration or after each iteration. We sometimes choose one over the other depending on whether or not we want at least one iteration to take place.

Our pre-tested structure is the while structure.

int countTo = 0;int startValue = 0;

Console.Write("What number do you want to count to?: ");countTo = int.Parse(Console.ReadLine());

while (startValue <= countTo){ Console.WriteLine(startValue); startValue++;}

As you can see, we need some extra things to similar a FOR loop with a condition control.

1) We need a value to compare against so that we can get a BOOLEAN result

2) We need to increase the ‘counter’ still by using the ‘++’ operator.

However, as you can see – the results look identical. The question is: What is more efficient?

string myName = "";const string storedName = "Ceredig";

while (myName != storedName){ Console.Write("Guess is my name? "); myName = Console.ReadLine();}

Console.WriteLine("This is my name!");

Here we repeat the user to enter data until it is correct/matches other data, and then allow the program to continue.

Page | 16

Page 17: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Our post-tested structure is the do..while loop.

bool quit = true;

do{Console.Write("Please enter a menu choice: ");}while (quit == false);

Here we see that the ‘quit’ Boolean data type (highlighted yellow) is the same in each case.Our check condition is whether or not the variable ‘quit’ is equal to false. If quit is false, then the loop will carry on.Because quit is set to true, quit == false would equate to false and therefore the loop would exit.

With a WHILE loop, because we check the condition first, the loop would not run…However, with a DO WHILE loop, because we check the condition AFTER the ‘do’ part, we would get output from the program.

bool quit = true;

while (quit == false){Console.Write("Please enter a menu choice: ");}

DO WHILE RESULT:

WHILE LOOP RESULT:

The main difference between these structures is whether or not we want to make at least one iteration take place.

When using condition-controlled iteration, we need to ensure that a stopping condition can be reached with our programming logic. Although there are ways to break out of iteration structures (break statement) and ways to skip to the next iteration (continue), we can avoid these if we consider our programming logic more carefully.

The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning or end of the loop to see the condition that needs to be met for it to continue. There is always a trade-off to be made between code that takes longer to write but that makes more sense when read and code that can be written succinctly. In most cases, the extra words in the source code are more useful to you and other programmers.

Page | 17

Page 18: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Programming Tasks D

1. Euclid’s highest common factor algorithm.

Write a program that implements the following pseudocode algorithm.

Set numerator variable to what the user enters Set denominator variable to what the user enters Set variable a to numerator Set variable b to denominator while a!=b If a>b Then Set a to a-b End if If b>a Then Set b to b-a End if Set hcf variable to a Output hcf

2. Extend the program from above so that it simplifies a fraction entered by a user.

3. Make a guessing game.

Write a program that generates a random number between 1 and 100. Allow the user to keep guessing the number until they have guessed the random number. If they enter a number lower than the number generated, tell them that their number is too low. Do the same if they enter a number higher than the number generated. Once they have guessed the number, tell them how many guesses they entered.

Extra ReadingThe following pages are a guide to most of the techniques required for C#. There is more on these pages than is included in this document.

http://multiwingspan.co.uk/cs.php

These pages described algorithms for enciphering and deciphering messages using programs. There are some ciphers that are part of the A level course and they have often been used for examination questions in the past. These pages describe the ciphers and offer pseudocode for writing them.

http://multiwingspan.co.uk/cipher.php

For even more challenge, there is no substitute for the Project Euler problems. Whilst some of these are quite accessible, they can get very tricky. Ideally a program should take less than a minute to complete. There is usually a mathematical or programming optimisation that allows for a result in less time though.

https://projecteuler.net/archives

Page | 18

Page 19: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Data RepresentationData representation was part of the specification for GCSE. It continues to be a topic in the A level specification and is extended a little from the GCSE material. Not all parts of the specification are included here but you can read up about them with a little online research should you wish.

Pure BinaryEverything in the computer is represented as a binary pattern. The component that makes modern computers possible is the transistor. We can think of the transistor as a switch, either open or closed. This gives us our values 1 and 0 that form the bits (binary digits) of our binary numbers.

The phrase ‘pure binary’ is used to describe a way of representing integers using as many place values as you need.

Understanding binary is about appreciating the importance of place value, the concept in Mathematics that allowed you to read and understand numbers when you were a primary school student. For binary integers, the bits and place values are,

128 64 32 16 8 4 2 Units

These are all powers of 2. The units column (or 1s) is 2 to the power of 0. Thereafter, moving to the left, the place values are increasing powers of 2.

The rightmost binary digit of a number is the one underneath the units column and is referred to as the least significant bit. The leftmost binary digit of a number is called the most significant bit. The term significant is being used in its mathematical sense here.

Binary Denary 128 64 32 16 8 4 2 1

42 0 0 1 0 1 0 1 0137 1 0 0 0 1 0 0 1

From the table above, we can see that the 8 bit binary representation of the denary number 42 is 00101010.

The 8 bit binary representation of the denary number 137 is 10001001.

Add up the place values which have a 1 beneath them and you see where these values come from.

Page | 19

Page 20: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Binary Question A

You may wish to print this. Any number that consists of only 1s and 0s can be assumed to be in binary. All others are denary.

1 2 3 4

5 6

7 8 9 10

11 12 13 14 15

16 17

18 19

20

Page | 20

Page 21: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Binary AdditionBinary addition follows the basic principles of column addition with digits being carried when the ‘limit’ of a column is reached. What changes from denary is that the largest value for a column is no longer a 9 but a 1.

This means,

0 + 0 = 0

0 + 1 = 1 + 0 = 1

1 + 1 = 10 (0, carry 1)

1 + 1 + 1 = 11 (1, carry 1)

In the following example, the binary numbers, 1011 (11 in denary) and 10010 (18 in denary) are added together.

When performing binary addition, you must lay your sums out this way. Do not work things out in your head or use any mental methods that you learned in little school. The potential for error is too great not to take care with something that is quite basic.

If, when adding numbers together, we end up with a total that needs an additional bit, our calculation has generated an overflow.

Binary Question B

1. 1 0 1 1 0 0 0 0 +0 0 1 0 0 0 0 1

2. 1 0 1 1 0 0 1 1 +0 0 1 0 0 1 0 1

3. 1 1 0 0 1 1 0 0 +0 1 0 1 0 1 0 1

4. 1 0 0 0 1 1 0 1 +0 0 1 0 1 1 1 1

Page | 21

Page 22: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Two’s ComplementComputers need to be able to represent negative as well as positive quantities. The method used for this is called two’s complement.

In denary, when we want to represent a negative number, we simply place a minus sign before it. Binary doesn't work that way.

The Two's Complement system is used to represent negative numbers in binary. The system works a bit like a milometer. If the milometer is set at 00000 and is turned back one mile, it would read 99999. A negative binary number always has a 1 as the first bit. This is often referred to as the sign bit.

It turns our place value table into,

-128 64 32 16 8 4 2 Units

With 8 bits, we can represent numbers from -128 to 127.

When using two’s complement, a negative number always has a 1 for the most significant bit, a positive number always has a 0.

There is a simple method to convert between positive and negative numbers and the same method works in both directions.

Flip all of the bits. Add 1.

Binary Question BConvert the following positive binary integers into negative binary integers using two’s complement form.

1. 00001111

2. 00011101

3. 01001000

4. 01110000

5. 01100110

6. 01010101

You can check your answers using the interactive on http://multiwingspan.co.uk/as1.php?page=bin.

Binary Subtraction

In order to carry out binary subtraction, we take advantage of the following equivalence,

a – b = a + -b

Given two binary integers, convert the one you are subtracting to two’s complement form and then add it to the one you are subtracting from.

Page | 22

Page 23: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

In denary, 12 – 6 is the same as saying 12 + (-6). We can use this knowledge to perform subtraction in binary.

0 0 1 1 0 0 0 0 -0 0 1 0 0 0 0 1

Find out the Two’s Complement form of the second number,

00100001 Flip bits = 11011110 Add 1 = 11011111

Now add this to the first number.

0 0 1 1 0 0 0 0 +1 1 0 1 1 1 1 10 0 0 0 1 1 1 11 1 1

We do not carry the last 1 when doing subtraction this way. Let’s check the method,

00110000 = 48 00100001 = 33 00001111 = 15 48 – 33 = 15

Binary Question C1. 01010101 – 00011011

2. 01110000 – 00011101

3. 01010101 – 00011011

4. 01110000 – 00011101

You can check your answers using the interactive on http://multiwingspan.co.uk/as1.php?page=bin.

Binary MultiplicationTo multiply by 2, shift all of the digits of the binary number one place value to the left. To multiply by 4, shift all of the digits two places to the left.

The left and right shift method is fine for multiplying and dividing by powers of two. You really need to use long multiplication, applying the same method that works in denary.

For example, 1001 * 0101

1 0 0 1 xLay the numbers out in columns

0 1 0 11 0 0 1 + Multiply the first number by the rightmost place value of the second.

1 0 0 1 0 0 Add 0s until next non-zero digit. Multiply the first number by this digit.1 0 1 1 0 1 Add together the part results to get the final answer.

Binary Question DMake a few questions of your own to do. Use the Windows calculator to check your answers by going to Programmer mode and setting the number base to binary.

Page | 23

Page 24: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Fixed Point Binary FractionsIf we extend the place values of our binary numbers to the right, we make negative powers of 2, which are fractions.

8 4 2 Units 1/2 1/4 1/8 1/160.5 0.25 0.125 0.0625

This method for representing fractions in binary is called fixed point binary. In order to work out the value of a bit pattern, we need to know the position of the binary point.

Binary Question EList all of the binary fractions that can be made if you have 4 bits after the binary point.

Bit Pattern Value Bit Pattern Value

0000.0001 0.0625

0000.0010 0.125

0000.0011 0.1875

At the bottom of this page, http://multiwingspan.co.uk/as1.php?page=bin, there is a description of a method that can be used to convert a decimal fraction that is not a power of 2 into fixed point binary.

Using the method, you will see that there are some numbers that need a small number of place values to represent in denary but that need large or infinite numbers of bits to do so in binary. The number of bits you choose to represent a number is called precision. This term means something slightly different to computer scientists than it does to mathematicians.

Page | 24

Page 25: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

HexadecimalHexadecimal is Base 16. That means that our place values are powers of 16 and that we can have digits with the value 0-15 in any column. Here are the numbers from 0 -15 in denary, binary and hexadecimal.

Denary Binary Hexadecimal0 0000 01 0001 12 0010 23 0011 34 0100 45 0101 56 0110 67 0111 78 1000 89 1001 9

10 1010 A11 1011 B12 1100 C13 1101 D14 1110 E15 1111 F

Notice that we need 4 bits to make all of the hexadecimal digits for each column. That makes it pretty easy to convert between binary and hexadecimal.

Page | 25

11000111

0111

7

C7

C

1100

Page 26: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Hexadecimal is used as a shorthand for binary. It allows programmers to use a more compact notation when using binary patterns. Since all information in computer systems is stored in binary, the use of hexadecimal makes no difference to storage or memory usage. It does reduce the number of digits to parse for a human reader by giving one hexadecimal digit for every four binary digits (nibble).

Hexadecimal Question AHex To Binary

1. B3 ……………………………………… 6. 9F ………………………………………

2. E2 ……………………………………… 7. 4D ………………………………………

3. F8 ……………………………………… 8. 1A ………………………………………

4. A9 ……………………………………… 9. 5D ………………………………………

5. 7C ……………………………………… 10. 6C ………………………………………

Binary to Hex

1. 00110011 ………………… 5. 01101101 …………………

2. 01010101 ………………… 6. 11011001 …………………

3. 00011101 ………………… 7. 11100011 …………………

4. 11110010 ………………… 8. 11110001 …………………

Page | 26

Page 27: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Extra ReadingA nice, challenging programming task would be to implement the method for converting denary fractions into fixed point binary.

The following page describes how floating point numbers are represented on computer systems,

http://multiwingspan.co.uk/a23.php?page=real

Implementing this in program code is a significant challenge – only for the brave souls. It is a nice extended task to do if you are looking for ideas.

Page | 27

Page 28: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Digital LogicLogic gates were part of the GCSE specification. At A level, we need to add a few more logic gates – important ones as it turns out.

We also need to look at some fundamental logic circuits.

The way we write expressions for logic gates at A level might be different to the way you had to read and write them at GCSE. You will also learn how to use algebra with the expressions.

In this section, you will find diagrams, truth tables and expressions for the main logic gates. You will also see a flip flop, and the adder circuits that are part of the A level specification. There will be some questions using logic circuits at the end of the section.

For exploring logic gates and the circuits you can make with them, I still like to use a program that is no longer being actively developed. It is called Logisim and can still be downloaded if you search online (it is a Java program and will need a Java runtime environment on your machine). The diagrams in this document were produced with that program. You can also create interactive logic circuits using a lot of online simulators directly in your browser.

Logic GatesAND Gate

A B Q0 0 00 1 01 0 01 1 1

In this circuit, both inputs A and B need to be set to 1 for the output Q to be a 1.

The expression for this circuit is Q=A . B

Page | 28

Page 29: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

OR GATE

A B Q0 0 00 1 11 0 11 1 1

In this circuit, either input A or B (or both) need to be set to 1 for the output Q to be a 1.

The expression for this circuit is Q=A+B

NOT GATE

A Q0 11 0

In this circuit, Q is always the inverse of A.

The expression for this circuit is Q=A

NAND Gate

A B QPage | 29

Page 30: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

0 0 10 1 11 0 11 1 0

NAND means ‘not AND’. Its output is the inverse of an AND gate. It is an important logic gate. All other logic gates can be made out of NAND gates. It is also used in flash memory circuits.

The expression for this circuit is Q=A . B

NOR Gate

A B Q0 0 10 1 01 0 01 1 0

NOR means ‘not OR’. Its output is the inverse of the NOR gate. Like the NAND gate, it is used to make flash memory and, using NOR gates only, all other logic gates can be made.

The expression for this circuit is Q=A+B

XOR Gate

Page | 30

Page 31: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

A B Q0 0 00 1 11 0 11 1 0

XOR means ‘exclusive OR’. You get an output of 1 if either input is a 1 but not both. This turns out to be useful enough for a special gate to exist as you will see with the adders.

The expression for this circuit is Q=A B

It can also be written as Q=A . B+A .B when the circuit is constructed using fundamental logic gates.

Flip FlopThe D-type flip flop is part of the specification for the A level. You just need to know that it exists and roughly how it works.

A flip-flop is made from combinations of logic gates and are used as units of memory within integrated circuits. Flip flops are also referred to as latches, because of the way you work with them electronically.

The input D is our data bit. This is the information we want to store. We set that high or low.

This circuit uses a rising edge triggered flip flop. The input C is called our clock. When the clock goes from low to high, our data bit is 'clocked in' and the output from the circuit at point Q reflects the value of the data bit we set.

The output only changes when the input C is changed from low to high. The edge that is rising is the voltage signal, were you to plot it with an oscilloscope. This means that the data bit that was set, can be read from the output for as long as that bit needs to be stored. This makes it a unit of memory in a logic circuit.

There are other types of flip flop (JK is very common).

Page | 31

Page 32: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Half Adder

The half adder circuit is a logic circuit which adds 2 bits together and outputs the SUM and the CARRY bits.

INPUTS OUTPUTSA B S C0 0 0 00 1 1 01 0 1 01 1 0 1

S=A⊕BS=A . B+A .B

C=A .B

Full Adder

Page | 32

Page 33: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

A full adder is a little more complex as a circuit. It is designed to be chained along with other full adders to make circuits which can do addition of more bits.

INPUTS OUTPUTSA B Cin S Cout

0 0 0 0 00 0 1 1 00 1 0 1 00 1 1 0 11 0 0 1 01 0 1 0 11 1 0 0 11 1 1 1 1

Cin = CARRY IN (FROM PREVIOUS ADDER), Cout= CARRY OUT

2 Bit Adder

This is a circuit which can add together two 2-bit numbers and report the result. To make an adding circuit, use a half adder for the least significant bit (units). You only need a half adder because there is no carry in on the least significant bit.

Page | 33

Page 34: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Each place value thereafter needs a full adder. To add a place value to this adding circuit, C1 is connected as the carry in to the next full adder.

Logic Gates Questions A

Page | 34

Page 35: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

2. A1, A2, B1, B2 and Q are Boolean variables.

a. Construct a truth table for the above circuit

A1 B1 A2 B2

C(A1 XOR B1)

D(A2 XOR B2)

Q(C NOR D)

0 0 0 00 0 0 10 0 1 00 0 1 10 1 0 00 1 0 10 1 1 00 1 1 11 0 0 01 0 0 11 0 1 01 0 1 11 1 0 01 1 0 11 1 1 01 1 1 1

b. Express Q in terms of A1, A2, B1, B2 for the logic gate circuit.

NOR = BA

EOR = .BABA.

Further Reading & ActivityDownload Logisim or use an online logic simulator to explore the circuits and logic gates mentioned in this section.

Make an adder that is capable of adding together two nibbles (4 bit numbers).

You can read ahead about Boolean algebra, a topic that you will cover in detail during your course. Don’t worry if it seems confusing when you try to learn it by yourself. It will end up being quite straightforward for you.

http://multiwingspan.co.uk/as2.php?page=boolean

Page | 35

D

C

Page 36: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Page | 36

Page 37: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Logic ProblemsThe ability to solve simple logic puzzles has been part of the specification for some time. At its heart, our subject is about solving problems. Not all problem solving is about programming.

Problem SolvingHere are some terms that can be used to discuss problems in general.

Term DefinitionGiven The initial situation - the starting pointGoal The desired outcome - what you want to achieveResources

The things that can be used to reach the goal are that impose constraints on how the goal can be reached

Problem A given where it is not obvious how to reach the goal

A problem is well-defined if there is a,

clearly defined initial situation, clearly defined goal, clearly defined set of resources and constraints, clear ownership - someone is responsible for arriving at the solution.

The last point is really important, not just for the general theory about problem solving but for approaching work in general.

We employ a range of strategies when solving problems. One common approach is called decomposition, where we break the problem up into parts that are simple to solve by themselves.

Another approach that can work is exhaustive searching. That means that we explore all candidate solutions to a problem before deciding which one or ones provide the true answer.

Logic Puzzles Tasks

1. Party Time

3 friends were on their way to a party. The following facts are true about their clothes,

a) Sally did not wear leather.b) The person in cotton was wearing yellow or blue.c) The person in blue wore leather or wool.d) The person in cotton was Jim or Fred.e) Sally was not in blue.f) Fred was in red or blue.

Match the person with the colour and material.

Person Material Colour

Sally

Jim

Page | 37

Page 38: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Fred

2. Whose pet is it?

Anita, Ben, Chuck, Debbie, Eliza and Fred each owned a pet. Their pets (not in order) were a parakeet, a cat, a dog, a fish, a parrot and an owl.

a) Eliza's neighbour, who was not Ben, owned a parrot.b) Fred did not own a flying creature.c) The parakeet owner lived next to Debbie.d) Ben did not own a four-legged pet.e) Both the owl and cat owners lived across the street from Eliza who lived next to the parakeet owner.f) The owl and parakeet owners lived at the opposite end of the street to Anita and Ben.

Work out who owned which pet.

Anita

Ben

Chuck

Debbie

Eliza

Fred

3. Great Golfers

Mr Peters, Mr Edwards and Mr Roberts are playing golf. Mr Peters remarks that he has just noticed that their 3 first names are 'Peter', 'Edward' and 'Robert'. "Yes", says one of the others, "I'd noticed that too, but none of us has the same surname as our first name. For example, my first name is 'Robert'".

What are the full names of the three golfers?

More Puzzles

Page | 38

Page 39: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

The following site is an infinite source of grid-based logic puzzles,

https://logic.puzzlebaron.com/

Page | 39

Page 40: Introduction  · Web view2020-05-29 · The advantage of this over the old Python classic ‘while true’ is that we end up with code that is easier to read. We can use the beginning

Thinking Like A Computer Scientist

In addition to the material in this document and the logic puzzles, there are other challenges that can get you into the habit of thinking like a computer scientist.

SudokuSudoku puzzles lend themselves to being solved using algorithms. The following site describes specific strategies that you can employ to solve Sudoku puzzles. I have yet to find a Sudoku puzzle in a mainstream newspaper that cannot be solved using only these strategies.

http://multiwingspan.co.uk/doku.php

When completing a puzzle, think about the algorithm you are using to find your next step. Think about the methodical way you scan columns, rows and mini-squares to find places where you can apply each strategy.

Twisty PuzzlesI have always found twisty puzzles like the Rubik’s cube to be interesting. As a youngster, I enjoyed the challenge of solving something difficult. As I got older, I learned to appreciate the mathematically and computationally interesting aspects of each of the puzzles. The maths involved in the puzzles and programmed solutions to them can be quite demanding.

The following pages describe a number of puzzles, their solutions and some other areas of interest.

http://multiwingspan.co.uk/puzzle.php

Electronics & MicrocontrollersElectronics is a good way to gain an appreciation of the implications of the hardware at the heart of our subject. It also provides a good context for writing programs.

The micro:bit is cheap and can be programmed in Python. There are a lot of ideas on,

http://multiwingspan.co.uk/micro.php

The Arduino is programmed in C++. The syntax and most of the core statements and structures are identical or virtually identical to those you will use in C#.

These pages are older but contain some ideas for programming some circuits for Arduinos.

http://multiwingspan.co.uk/arduino.php.

Write ProgramsComputer Science is about programming. Whenever you are faced with a problem or challenge that can be solved with a program, you should be trying to do that.

Make programs to solve problems related to your other subject choices for A level, your hobbies or just to annoy your siblings.

If you play a game that allows you to write mods, have a go. It’s not as hard as you think to get some kind of result and it gets you thinking like a developer for a while.

In short, be a programmer, write programs.

Page | 40