Teaching Computing to GCSE Session 1 Introduction Theory






















- Slides: 22

Teaching Computing to GCSE Session 1 Introduction Theory: Binary Arithmetic Practical: KS 3 Python Programming Recap

Course Outline Computing Theory (5: 00 – 6: 00) Programming in Python (6: 00 – 7: 30) Binary arithmetic Python programming KS 3 recap Truth tables/logic diagrams For loops and while loops CPU Architecture 1 -D & 2 -D arrays (lists) The internet & protocols Tracing and pseudocode Cybersecurity Functions & parameters Searching algorithms Exception handling and debugging Sorting algorithms Testing programs High level/low level languages File handling & CSV files Consolidation: Programming for GCSE with longer tasks

Specification Content OCR • How to add two 8 bit binary numbers and explain overflow errors which may occur • Binary shifts AQA • Be able to add together up to three binary numbers • Be able to apply a binary shift to a binary number • Describe situations where binary shifts can be used Edexcel • Understand how computers represent and manipulate numbers (unsigned integers, signed integers (sign and magnitude, two’s complement)) • Understand how to perform binary arithmetic (add, shifts (logical and arithmetic)) and understand the concept of overflow

Binary Recap Each place in a binary number has a value. These values go up in multiples of 2. 128 64 32 16 8 4 2 1 0 0 0 16 + 4 = 20 We convert a binary number to decimal (aka denary) by adding the place values of the columns that contain a 1.

Binary Addition Recap For the AQA specification you need to be able to add together up to three binary numbers. Rules: 0+1 = 1 1+1 = 10 (write down 0, carry 1) 1+1+1 = 11 (write down 1, carry 1) 1+1+1+1 = 100 (write down 0, carry 1) 1 1 0 1 0 1 1 0 0 0 1 1 0 0 0

Negative Numbers There are two different methods of representing negative numbers in binary: Sign and Magnitude Two’s Complement In both systems the most significant bit is known as the ‘sign bit’.

Sign and Magnitude Sign and magnitude is the simplest way of representing negative numbers in binary. In sign and magnitude the sign bit doesn’t have a value, it simply tells us whether the number is positive or negative. 0 = plus (+) 1 = minus (-) Sign 64 32 16 8 4 2 1 1 0 0 = -20

Activity 1 a Convert the following binary numbers that use sign and magnitude representation to decimal. Sign 64 32 16 8 4 2 1 1 1 0 0 0 0 1 1 1 0 0 1 1 1 1 Decimal

Activity 1 b Convert the following decimal numbers to binary numbers that use sign and magnitude representation to decimal. Use paper for working out. Decimal -73 -91 46 102 Sign 64 32 16 8 4 2 1

The Problem with Sign and Magnitude Sign and magnitude might be simple, however it causes problems when performing binary addition. Two’s complement is an alternative method of representing negative binary numbers that works with binary addition.

Two’s Complement In two’s complement the sign bit is a negative number. -128 64 32 16 8 4 2 1 1 0 0 Just like a normal binary number, we convert it to decimal by adding the place values together. -128 + 64 + 32 + 8 + 4 = -20

Flipping the Bits We can easily work out the positive equivalent of a negative two’s complement number using the ‘flipping the bits’ method. 128 64 32 16 8 4 2 1 Original Number 1 1 1 0 0 Flip the bits 0 0 0 1 1 Add 1 0 0 0 1 0 0 = -20 20 Finally we add a minus sign, as the original number is negative.

Activity 2 a Convert the following binary numbers that use two’s complement representation to decimal. Sign 64 32 16 8 4 2 1 1 0 0 0 1 1 0 1 0 1 1 1 1 Decimal

Activity 2 b Convert the following decimal numbers to binary numbers that use two’s complement representation to decimal. Use paper for working out. Decimal -73 -91 46 102 Sign 64 32 16 8 4 2 1

Logical Shift Logical shift can be used to easily multiply and divide number by powers of 2. A logical left shift of 1 multiplies the number by 2. A logical right shift of 1 divides the number by 2. We pad out any empty places with 0 s. 128 64 32 16 8 4 2 1 0 0 0 0 0 1 0 0 0 0

Activity 3 a) Perform a logical left shift of 1 on this number. Original 128 64 32 16 8 4 2 1 0 0 1 1 0 0 Decimal Shifted a) Perform a logical left shift of 2 on this number. Original Shifted 128 64 32 16 8 4 2 1 0 0 1 Decimal

Activity 4 a) Perform a logical right shift of 1 on this number. Original 128 64 32 16 8 4 2 1 0 0 1 1 0 0 Decimal Shifted a) Perform a logical right shift of 2 on this number. Original Shifted 128 64 32 16 8 4 2 1 1 0 0 0 Decimal

Shifting Signed Integers Logical shift won’t work with negative numbers represented in two’s complement binary. When we shift the number to the right we loose the sign bit, giving us the wrong result. -128 64 32 16 8 4 2 1 1 0 0 0 0 0 1 0 0 0 = -112 = 72

Arithmetic Shift Arithmetic shift solves the problem of shifting a negative two’s complement to the right. With arithmetic shift, when shifting a number to the right we pad out the empty spaces with a copy of the sign bit. -128 64 32 16 8 4 2 1 1 0 0 0 0 1 1 0 0 0 When shifting to the left we still fill the empty places with 0 s. = -112 = -56

Activity 5 a) Perform an right arithmetic shift of 1 on this two’s complement number. Original -128 64 32 16 8 4 2 1 1 0 0 0 0 Decimal Shifted a) Perform a right arithmetic shift of 2 on this two’s complement number. Original Shifted -128 64 32 16 8 4 2 1 0 0 0 1 1 0 0 0 Decimal

Activity 6 a) Perform an left arithmetic shift of 1 on this two’s complement number. Original -128 64 32 16 8 4 2 1 0 0 0 Decimal Shifted a) Perform a left arithmetic shift of 2 on this two’s complement number. Original Shifted -128 64 32 16 8 4 2 1 0 0 0 1 1 Decimal

Break After the break we will recap Python Programming for KS 3