### CS28 : Do the Math We are fairly comfortable doing maths with base 10 numbers because that's what we've always done. However, computers do maths with base 2 numbers which, in principle, is just as easy.

We are learning ...
So that we can ...
• Add up to three binary numbers together
• Use binary shift to multiply and divide binary numbers
• Explain how overflow (and underflow) can occur

 CGP The Revision Guide Page 68, 69 CGP Exam Practice Workbook Page 77, 78 Activity 1Addition              When you add two single digit numbers together and the result of the addition is the same as or larger than the number base you are working in, you have to perform a carry operation.

Where we refresh the rules for adding numbers with carry On the worksheet The main rules of binary addition are shown in the popup. Look carefully at it and make your you understand.

 The rules of binary addition` 0 + 0     =  0``0 + 1     =  1``1 + 0     =  1``1 + 1     = 10 (0 carry 1)``1 + 1 + 1 = 11 (1 carry 1)`

Task 1.2 Some practice in base 2!
Where we learn how to add numbers in binary Watch the animation carefully

Computers generally store both the number to be added and the results of the addition in a restricted number of bits (for us it's going to be 8). Watch the following example of addition of two 8 bit binary numbers carefully ...  In your notebook / on paper

Now, try adding together the following pairs of binary (base 2) numbers. Show all working in your books and lay out your working like the example, using a grid. Remember, you only have 8 bits in which to store the numbers and the answer. Ask your teacher to demonstrate the first question to you if you don't know what to do.
1. 00101010 + 01000100
2. 01100010 + 01000100
3. 01111011 + 00000010
4. 00100000 + 01110111
5. 11101010 + 01000100 Whoa! What happened with question 5? You should have discovered that you can't fit the result of the calculation in the 8 bit space you provided in the grid - you need 9 bits to store the answer. This is called overflow and is a bit like trying to fit a litre of water in a pint glass!  Where we learn how to add denary numbers together in binary (!) Watch the animation carefully

In order to practice your denary to binary conversion and your binary addition skills, have a go at these questions. First you have to convert each denary number to its binary equivalent (and represent this in 8 bits) and the perform a binary addition. Watch the following example carefully and use it to help you lay out your work.  In your notebook / on paper

Perform the following binary addition questions, using the method shown.
1. 53 + 193
2. 99 + 111
3. 78 + 34
4. 13 + 33
5. 45 + 127 Activity 2Binary multiplication using shifts              In denary, if we multiply or divide a number by 10, we shift the digits to the left or the right ...

23 x 10 = 230 x 10 = 2300 x 10 = 23000 ÷ 10 = 2300 ÷ 10 = 230 ÷ 10 = 23

We have to be careful not to overflow during multiplication and underflow during division. Underflow occurs when the number gets too small to represent using the place values we have available in our number. Task 2.1 Binary shifts multiply and divide by 2
Where we learn how to perform multiplication and division of binary numbers

In the same way that denary shifts multiply and divide by 10, a binary shift multiply and divide by 2. Essentially, this is the same as doubling and halvingCarry out the following tasks using The Python programming environment. There is no need to evidence the Python coding but you must respond to the activities where suggested.  Open the Python programming environment, IDLE. Python bitwise operators

Python uses two of its bitwise operators to implement binary shifts.

 `x << y    ` ... shifts x to the left by y bits multiplying x by 2y `x >> y    ` ... shifts x to the right by y bits dividing x by 2y At the prompt

Type the following commands at the prompt, pressing the  ENTER  key after each one.

`>>> a = 60`
`>>> a = a << 1`
`>>> print(a)`

The result of this left shift operation should be `120`. Convince yourself that all that has happened here is that the binary equivalent for 60 has been 'shifted' 1 place to the left by working out the binary equivalent of 120 and comparing it to the binary equivalent of 60 (shown above).

Left binary shift multiplies by 2 (the base) At the prompt

In the same way, right binary shift divides by 2 (the base). Try the following simple command, pressing  ENTER  afterwards.

`>>> a = a >> 2`
`>>> print(a)`

Right binary shift divides by 2 (the base) At the prompt

It is possible to see what is happening visually by using some fancy string formatting. Type the following commands at the prompt, pressing the  ENTER  key after each one.

`>>> a = 0b00111100`
`>>> print('{0:08b}'.format(a))`>>> print(a)
`>>> a = a << 1`
`>>> print('{0:08b}'.format(a))`>>> print(a)
`>>> a = a >> 2`
`>>> print('{0:08b}'.format(a))>>> print(a)`

Starting the value of `a` with '`0b`' tells Python to treat the number which follow as 'b'inary. Can you see what the string format does? Try increasing the value of the number `8` in the format specifier and see what happens. In your notebooks / on paper

Write about what you have done and what you have learned about in your notebooks. You will have to include some code samples in your explanation - use a highlighter pen to signify that they are code snippets rather than normal writing.   At the prompt

Now perform increasing right shifts (`>>`) on `a` starting at `2` and getting bigger. What happens eventuallyCan you explain what happens? Assessment Task (Homework) Create your own challenge sheet for next lesson. Come up with some binary maths problems and write them down on a piece of A4 paper. Bring them to your next lesson so you can try them out on your friends. Grading rubric MASTER : You have brought a challenging set of questions to the lesson covering all aspects of binary arithmetic. APPRENTICE : You have brought some addition and multiplication problems with you. NOVICE : You have brought some simple addition problems with you. Hungry for more?

Investigate some of the other Python bitwise operators.

FAQ

Q : How do you do roots and powers using binary
A : Roots and powers are complicated and tend to use specially written functions provided by the math library.

Q : How do you multiply and divide by 3, 5, 7 etc
A : To multiply by three, you would perform a single left shift and then add the original number. To multiply by 5, you would perform a two place left shift and add the original number. For seven you would be better performing a 3 place left shift and then adding the negative value of the original number, but that's A Level work.