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 ...- About binary mathematics
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
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. Task 1.1 Rules of additionWhere we refresh the rules for adding numbers with carry
On the worksheetDownload the
Rules of Addition worksheet (if you have not already been given a copy by your teacher) and complete the activities as a group.The main rules of binary addition are shown in the popup. Look carefully at it and make your you understand.
Task 1.2 Some practice in base 2!Where we learn how to add numbers in binary
Watch the animation carefullyComputers 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 paperNow, 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.- 00101010 + 01000100
- 01100010 + 01000100
- 01111011 + 00000010
- 00100000 + 01110111
- 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!Task 1.3 Binary conversion and additionWhere we learn how to add denary numbers together in binary (!)
Watch the animation carefullyIn 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 paperPerform the following binary addition questions, using the method shown.
- 53 + 193
- 99 + 111
- 78 + 34
- 13 + 33
- 45 + 127
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 2Where 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 halving. Carry 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.Get ready to codePython bitwise operatorsPython uses two of its bitwise operators to implement binary shifts.
At the promptType 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 promptIn 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 promptIt 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))` 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 paperWrite 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 promptNow perform increasing right shifts ( `>>` ) on `a` starting at `2` and getting bigger. What happens eventually? Can you explain what happens?
Click to load key word list to help you make your own flash cards
Investigate some of the other Python bitwise operators.FAQQ : How do you do roots and powers using binaryA : 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 etcA : 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. |