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)
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.
The range of an unsigned binary number is 0 to 2^{n}-1
Two's complementRepresenting 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.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 ...
The range of a signed, two's complement, binary number is -2^{n-1} to +2^{n-1}-1Sign and magnitudeThere 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 ...
Fixed point binaryAs 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 ...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.
least number of bits you can for this, leaving the most for the fractional part, hence maximising accuracy.
Range versus accuracySince 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 ...
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 representation. The 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 ...The size of the mantissa determines the accuracy of a number,the size of the exponent determines it's range.
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.
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.
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 ...
Addition of fixed point numbersEssentially, the same as addition of any other binary number. We do need to be careful about
range and precision however. Consider this example ...
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!
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!
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?
NEED TO ADD SUBTRACTION OF FLOATING POINT NUMBERS FOR OCR
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.
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.
END OF TOPIC ASSESSMENT |