CS06 : Maths with two fingers


So we add, subtract, multiply and divide using our fingers and the beautiful, if slightly familiar, base 10 number system. What about computers? They count in binary so how do you do maths with that?

We are learning ...
  • About binary coding used for the representation of numbers
So that we can ...
  • Describe unsigned binary
  • Represent signed binary
    - two's complement
    - sign and magnitude
  • Represent fractions in binary using fixed point notation
  • About representing fractions using floating point notation
    - Normalisation
    - Advantage / disadvantages of fixed and floating point notation (range, precision, speed of calculation)
    - Two's complement mantissa and exponent
  • State the range of a binary number
  • Perform binary mathematics
    - Unsigned binary (addition, multiplication)
    - Signed binary (subtraction)
    Floating point arithmetic (signed / unsigned - addition / subtraction)

Activity 1 Unsigned binary is simple! (15)

The binary representation we have met previously is actually called unsigned binary because it has no sign (obvious really). In fact, it is assumed that unsigned binary numbers are positive and do not require a '+' sign at the beginning.

Task 1.1 How many bits and what range?

Here is a 'bit' of revision. How many denary values can be represented using the following numbers of bits?
  • 4 bits
  • 6 bits
  • 8 bits
  • 10 bits
  • 12 bits
The range of a binary number is a little different. Range is the largest to the smallest value that can be represented and all binary numbers start at zero. What is the range of denary numbers which can be represented by the following numbers of bits?
  • 4 bits
  • 6 bits
  • 8 bits
  • 10 bits
  • 12 bits

OUTCOME : An oral confirmation of the connection between denary and binary

The range of an unsigned binary number is 0 to 2n-1


Activity 2 Negative numbers - signed binary (70)

Two's complement

Representing negative numbers in binary is usually done using a system called two's complement. It is relatively easy to convert a negative denary number to two's complement.

Step 1 : Represent the number in unsigned binary.
Step 2 : Invert all the bits - 1's become 0 and 0's become 1.
Step 3 : Add '1' to the number.

https://drive.google.com/file/d/0B83yXMOilskaNVlZc0FJZkI5LWM/preview
Click to enlarge

This has the effect of negating the value of the most significant bit. In an 8 bit binary number, this means that the MSB (most significant bit) is worth -128 rather than +128. This means that the the whole range of numbers we can represent is essentially shifted down by 128 places. In other words, an 8 bit signed number has a range of -128 to +127 whereas an unsigned number has a range of 0 to 255. 

They both have the capacity to represent 256 values however - remember that! Look at the popup ...

https://drive.google.com/file/d/0B83yXMOilskaR1ZaMFFRSk5DMXM/preview
Click to enlarge

Task 2.1 Representing negative numbers

Convert the following into 8 bit two’s complement form (signed 8 bit integers) using the two's complement method.
  • -26
  • -23
  • -120
  • -65
  • -112
Converting signed binary numbers back to denary is easy to - just make sure that you negate the value of the most significant bit in your number. Convert the following two's complement binary numbers back to denary.
  • 11110001
  • 10011011
  • 11001100
  • 11110111
  • 11110011

OUTCOME : Worked examples of two's complement representation for negative denary numbers


The range of a signed, two's complement, binary number is -2n-1 to +2n-1-1


Sign and magnitude

There is another, simpler way to represent negative numbers in binary which is called sign and magnitude. A number represented in sign and magnitude notation looks very different to one represented in two's complement so it's very important that we know in advance which system we are using.

With sign and magnitude method, the MSB now represents the sign of the number not its value (usually 0 for positive and 1 for negative) whilst the rest of the number is the magnitude of the number. This time, however, the range of a sign and magnitude number is -127 to +127, not quite the same as two's complement. Look at the popup ...

https://drive.google.com/file/d/0B83yXMOilskaM2pKbDFvX3ZpcG8/preview
Click to enlarge

Task 2.2 Sign and magnitude

Convert the following denary numbers into sign and magnitude format.
  • -56
  • -120
  • -48
  • -100
  • 115
Convert the following sign and magnitude numbers into denary.
  • 10100001
  • 11101111
  • 10101010
  • 10000000
  • 01111111

OUTCOME : Worked examples of sign and magnitude conversions


Activity 3 Representing fractional numbers using fixed point format (70)

Fixed point binary

As we work from the most significant bit of any binary number to the least significant, each bit value becomes worth half the value of the previous. This pattern continues beyond the first bit in a traditional binary integer into the fractional zone. The 'point' in a binary number is called a 'binary point' rather than a 'decimal point' (or more generally radix point) and, just like in a denary number, doesn't occupy any space in the binary string. The programmer and the computer need to agree before hand where the binary point will be and it remains fixed in that position (hence the name). It's position is implied. Look at the popup ...

https://drive.google.com/file/d/0B83yXMOilskaTmVqQTRiWTBRSlU/preview
Click to enlarge

The representation method for fixed point numbers is identical to the one you have used for unsigned and signed numbers - you've just got to remember the position of the radix point.

https://drive.google.com/file/d/0B83yXMOilskaenB5WndEMDRBVnc/preview
Click to enlarge (and I'd probably print it for my notebook)

Task 3.1 Fixed point binary

Write the following fixed point binary numbers as decimals ...
  • 110.01000
  • 010.11000
  • 00011.101
  • 1101.0110
  • 010110.01

OUTCOME : Worked examples of binary to decimal conversion


Converting denary numbers into fixed point binary is the same as converting any denary number to binary. Just remember to start from the most significant bit and work to the right until you have the most accurate value you can represent in the given number of bits. Start with the integer part and make sure you use the least number of bits you can for this, leaving the most for the fractional part, hence maximising accuracy.


Task 3.2 Decimal to fixed point binary

Write the following as binary numbers using 8 bits with the least number of bits to the left of the binary point ...
  • 5.25
  • 13.75
  • 11.375
  • 26.625
  • 112.5

OUTCOME : Worked examples of decimal to fixed point binary conversions


Range versus accuracy

Since we can never accurately represent some fractional numbers in binary (for instance 1/3), we often have to accept a certain amount of inaccuracy. However, it is possible to change the position of the binary point to alter the balance between range and accuracy. Look at the popup ...

https://drive.google.com/file/d/0B83yXMOilskaLUpiSTFndlk3YWc/view?usp=drive_web
Click to enlarge

Task 3.3 Range vs Accuracy

Calculate the potential denary range and the accuracy of the following fixed point binary number patterns. Use the example in the popup to help you if you need it.
  • 000.00000 to 111.11111
  • 00000.000 to 11111.111
  • 000000.00 to 111111.11

OUTCOME : Statement (and demonstration of understanding) of range and accuracy of fixed point binary.


Activity 4 Representing fractional numbers using floating point format (150)
 A Level Only

One of the biggest issues with fixed point binary representation is the trade off required between range and accuracy. This problem is partially fixed through the use of floating point binary representationThe term floating point refers to the fact that the binary point can "float" : that is, it can be placed anywhere in the number.

Floating point numbers are known as real numbers and have the same format as standard form numbers that you have met in maths. Consider the following three popups. Your teacher will likely explain them to you ...

https://drive.google.com/file/d/0B83yXMOilskaVUozOE9SVTRSb0E/view?usp=drive_web
Click to enlarge

https://drive.google.com/file/d/0B83yXMOilskabUlRVmVMX3gyMGM/view?usp=drive_web
Click to enlarge

https://drive.google.com/file/d/0B83yXMOilskacXNneXdrUWg4Q0E/view?usp=drive_web
Click to enlarge

The size of the mantissa determines the accuracy of a number,
the size of the exponent determines it's range.

Task 4.1 An interesting consequence of floating point arithmetic

There are some interesting consequences of floating point arithmetic. One of the most dramatic happened in 1996 in French Guiana. Watch the video ...

Ariane 5 Rocket Disaster

... then download the read the article called A Bug and a Crash by James Gleick which explains why the disaster occurred. What has this got to do with floating point arithmetic?

OUTCOME : No specific outcome, just an awareness of some interesting stuff about rockets.


There are 4 possible situations we might come across and each one requires a slightly different method. Grab yourself a copy of the Worked examples information sheet before you start the next set of tasks. It contains information on ...
  • Denary to floating point binary conversions
  • Floating point binary to denary conversions
  • Rounding errors (don't forget about these!)
Oh, and I imagine that ...


All four of these examples generate normalised numbers. Normalisation requires that the start of any floating point binary number starts with 0.1xxxx if the mantissa is positive (and normalised) or 1.0xxxx if the mantissa is negative (and normalised). 

Normalisation maximises precision. 

Task 4.2 Floating point exercises

First ...

Download and read the Worked Examples infosheet from the lesson resources.

Denary to floating point binary conversions

Using two's complement format with 6 bit exponent and 10 bit mantissa, convert the following denary numbers to normalised floating point binary. I've given you three examples of each - when you have finished, make up some examples of your own to try on your friends (if you have any left by then).
  • Positive number greater than one

    17.125
    14.75
    56.625

  • Negative number greater than one

    -196.5
    -27.125
    -13.96875

  • Positive number smaller than one

    0.3125
    0.046875
    0.203125

  • Negative number smaller than one

    -0.5625
    -0.125
    -0.28125

Floating point binary to denary conversions

Using 2's complement format with a 6 bit exponent and a 10 bit mantissa, convert the following normalised floating point binary numbers to denary numbers. If you want more practice, try converting your answers to the first part back to denary.
  • 0 110000000  000101
  • 1 000000000  001001
  • 0 100011000  001010
  • 0 100000000  011111
  • 1 011000000  111101 (be careful - denormalise with '1'!)
  • 1 000000000  111110 (be careful - denormalise with '1'!)


Rounding errors

Calculate the absolute, relative and percentage errors in the floating point representation of the following denary numbers. Represent each number as a floating point number with a 6 bit exponent and 10 bit mantissa before you do this, because if you don't, you'll miss off the sign bit and get the wrong answer!

HINT : You need to find one number above and one number below the target figure and selecting the value closest. This might involve you adding or subtracting one 'least significant bit's worth ...
  • 24.3 (Try this and check your answers with the handout)
  • 66.7 
  • 123.9
  • 14.37
  • 36.074

Classification

Classify the following numbers as positive / negative and normalised / unnormalised.
  • 0 111100111 011111
  • 0 001101100 111111
  • 1 011111100 010000
  • 1 111011000 000111
  • 0 000100000 010100

OUTCOME : A set of worked examples on floating point binary conversions, rounding errors and classification.



Activity 5 Same string, different meanings (30)

So, any particular binary string could have a variety of different meanings ...
  • Unsigned integer
  • Signed integer represented using two's complement
  • Signed integer represented using sign and magnitude
  • Fixed point number with the radix point in any variety of positions
  • Floating point number made up from a mantissa and an exponent [A Level Only]
Therefore it is vital that we know how the string has been constructed before we attempt to interpret it.

Task 5.1 What is this?

Consider the following binary string ...

10110101

What denary value would this represent if it were an :
  • Unsigned integer
  • Signed integer represented using two's complement
  • Signed integer represented using sign and magnitude
  • Fixed point number with the radix point in the centre of the string
  • Floating point binary number with a 5 bit mantissa and a 3 bit exponent [A Level Only]
Make sure you write up what you have found out in your notebooks so that you can revise from it.


OUTCOME : The same binary string and a variety of different denary meanings


Activity 6 Addition (90)

Adding binary numbers is the same, in principle, as adding numbers in any other base. You should have covered simple binary addition at GCSE level, so I'll assume that and just give you some practice questions to do!

A quick reminder, however, of the rules ...

0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0 carry 1

Task 6.1 Binary addition

Perform the following binary addition. Remember to show all your working ...
  • 00011010 + 00000101
  • 00001001 + 00000101
  • 00010110 + 00001011
  • 00101010 + 00011110
  • 01011000 + 00001110

OUTCOME : Worked examples of binary addition.


Addition of fixed point numbers

Essentially, the same as addition of any other binary number. We do need to be careful about range and precision however. Consider this example ...

Animation showing method for addition of fixed point numbers

Task 6.2 Addition of fixed point numbers

Perform the following fixed point binary addition. Remember to show all your working and not to allocated a bit place for the radix point!
  • 101.00110 + 11.110010
  • 11011.011 + 1011.0111
  • 001111.11 + 101.10000
  • 1000.0100 + 1101.0101
  • 111100.01 + 0.0010100

OUTCOME : Worked examples of fixed point binary addition


Addition of floating point numbers [A Level Only]

OK! I simply have run out of steam now - read through the following article which explains how to add two floating point numbers together. That should teach you!

Task 6.3 Addition of floating point numbers

Because I've run out of steam, I would like you to work in pairs to design a demonstration which you can use to teach another group of students how to perform floating point binary addition. This will involve you creating at least one worked example and a set of exercises which you can ask the other set of students to work through. You can assess their understanding and they can assess your teaching skills!

OUTCOME : Teaching notes and assessment to teach floating point addition.


Activity 7 Multiplication (30)

Multiplication in binary is actually easier than it is in other number bases because you only ever need to multiply by one! Technically, you are performing a binary shift operation when you do this (you should have learnt about this at GCSE level).

Animation showing method for multiplication of binary numbers

The example shows how an overflow can occur - in this case the overflow digit is 0 so it makes no difference to the outcome, however, if the overflow was 1, that would be a different story!

Task 7.1 Binary multiplication

Perform the following multiplication. Remember to show all your working ...
  • 00000110 × 00000011
  • 00001001 × 00000101
  • 00001100 × 00000110
  • 00110000 × 00000001 
  • 01011011 × 00000100 

OUTCOME : Worked examples to some binary multiplication exercises


Activity 8 Subtraction (30)

Subtraction is actually the same as addition of a negative number. So 14 - 6 is the same as 14 + (-6). Does this give you a clue how to do it?


Task 8.1 Binary subtraction

Perform the following binary subtraction. Remember to show all your working ...
  • 18 - 9
  • 26 - 15
  • 17 - 34
  • 56 - 78
  • 9 - 124
Did you figure out what to do with the last two?


OUTCOME : Worked answers to some binary subtraction calculations.


NEED TO ADD SUBTRACTION OF FLOATING POINT NUMBERS FOR OCR

Activity 9 Consequences (25)

Errors which can occur during binary arithmetic are ...
  • Cancellation : Adding a very small number to a very large number will not change the result
  • Underflow : The result of a calculation is too small to fit into the available number of bits
  • Overflow : The result of a calculation is too big to fit into the available number of bits.
  • Rounding : Some number just can't be represented using binary. Rounding errors can be represented in absolute, relative and percentage forms.

Task 9.1 Examples of consequences

Try to come up with your own examples for each of these error types.

OUTCOME : Examples for cancellation, overflow, underflow and rounding errors.


Extension Activities 

How about these?
  • Design  a spreadsheet which will convert denary numbers into two's complement form (and vice versa);

  • Write a Python program which will to the same thing;

  • Write a Python program to perform binary maths.

  • In your programming language of choice, write a program which will convert a normalised floating point binary number to decimal and vice versa.  Your program should ask the user for the format of the number (mantissa, exponent), the direction of conversion and finally the number. The program should output the number in a suitable format.

What's next?

Before you hand your book in for checking, make sure you have completed all the work required and that your book is tidy and organised. Your book will be checked to make sure it is complete and you will be given a spicy grade for effort.

END OF TOPIC ASSESSMENT