Addition in Binary and Hexadecimal: 0 + 0 0 0 + 1 1 1 + 0 1 1 + 1 0 Carry 1
Addition in Binary and Hexadecimal: 0 + 0 0 0 + 1 1 1 + 0 1 1 + 1 0 Carry 1
Student name:
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0 carry 1.
Example 1:
0 1 0 0 1 1 0 1
+ 0 0 0 0 0 1 1 0
carry -> 1 1
result -> 0 1 0 1 0 0 1 1
If you are adding in columns and you have to do 1 + 1 + carry then the result will be 1 carry 1 to the
next column:
0 0 0 0 1 1 1 1
+ 0 0 0 0 0 1 1 0
carry -> 1 1 1
result -> 0 0 0 1 0 1 0 1
Confirm that these additions are correct by converting the numbers to base 10. Try these:
1 0 0 0 1 0 1 0
+ 0 0 0 1 1 1 0 1
carry ->
result ->
1 1 0 0 1 1 1 1
+ 0 0 1 0 0 0 1 1
carry ->
result ->
1 1 1 1 0 0 0 1
+ 0 0 0 0 1 1 1 1
carry ->
result ->
© 2007 Richard Jones, PO BOX 246, Cambridge, New Zealand. See: http://www.ib-computing.com/ for more worksheets
Page: 2 of 15
Student name:
What happened in the last example?
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
Imagine now that the three examples given above are in two’s complement form (the last value is the
same magnitude but negative – ie -128).
Convert to decimal and see what the examples tell you. Comment on the last example again:
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
We conclude that when subtracting two numbers by adding a two’s complement form positive number
to a two’s complement form negative number, that any overflow beyond the MSB can be ignored. This
is not true, of course, for addition.
0 0 1 A
0 2 0 9
carry -> 1
result -> 0 2 2 3
When we add the first column 9 + A we clearly go beyond F – the largest hex symbol we can have. I
got three by starting my left-hand thumb at A and then counting round 9 fingers (not forgetting 0) to
get the 3. Then of course we need to carry to the next column.
This is a bit tricky if you have to add, say C and D – you need more than 10 fingers (unless you can
“wrap around” of course.
The easiest way (I find) is to write down the numbers on a peice of paper (which you can do in the
exam – remembering you won’t have a calculator with you), and count around:
0 1 2 3 4 5 6 7 8 9 A B C D E F
start
here
Now, count around 1 on D, 2 on E, 3 on F, 4 on 0 and so on, until you count to D; you should end up
on 9.
© 2007 Richard Jones, PO BOX 246, Cambridge, New Zealand. See: http://www.ib-computing.com/ for more worksheets
Page: 3 of 15
Student name:
C + D in hex gives 19, carrying the 1. Is that right? Convert to decimal and check.
0 0 0 C
0 0 0 D
carry -> 1
result -> 0 0 1 9
Of course, some people with a natural aptitude for number work will be able to do this in their heads –
that’s OK, we don’t mind, we have other talents don’t we?
Try these - no calculators allowed! (Except for your poor old teacher, that is):
0 2 9 A
0 2 0 B
carry ->
result ->
0 F 9 0
0 2 9 D
carry ->
result ->
© 2007 Richard Jones, PO BOX 246, Cambridge, New Zealand. See: http://www.ib-computing.com/ for more worksheets
Page: 4 of 15
Student name:
234.56
So, for binary it is just an extension of the column weighting system to beyond the “bicimal” point
where the weights are 1/2, 1/4, 1/16 etc.
In a similar way to converting decimal numbers to binary, where we used repeated division by 2, we
can convert decimal fractions to binary fractions by repeated multiplication:
0.20 * 2 = 0.40 0
0.40 * 2 = 0.80 0
0.80 * 2 = 1.60 1
The pattern will now repeat itself. Depending on how many bits we want to store, we truncate or round
this result - let’s use 5 bits:
The rounded version represents 1/2 + 1/8 + 1/32 = 21/32 = 0.65625 so this is not a very accurate way to
store real numbers. What does the truncated version evaluate to as a decimal fraction?
-----------------------------------------------------------------------------------------------------------------
A number such as
101010.01011
is 42.34375 in decimal. We can obtain the negative two’s complement representation in exactly the
same was as was done for integers:
Using 8 bits for the whole number part and 5 bits for the fractional part, then 42.34375 is:
Student name:
Using the above 13 bit representation, convert the following numbers to binary, truncate any bits after 5
beyond the bicimal point, if neccessary.
a) 1.7
You can see that fixed-point representation has some limitations, just as it has in decimal. Really
large and really small numbers are hard to represent unless you’re prepared to allocate very many bits
of storage.
© 2007 Richard Jones, PO BOX 246, Cambridge, New Zealand. See: http://www.ib-computing.com/ for more worksheets
Page: 6 of 15
Student name:
Using the decimal number, 363.499 normalize it, then identify the mantissa and the exponent:
To represent binary floating-point numbers we will use a binary (fractional) mantissa and an integer
exponent which both use two’s complement representation in 16-bits (more bits would be allocated in a
real computer system).
A possible compromise between a large mantissa (greater accuracy) and a large exponent (greater
range) could be to allocate 10 bits for the mantissa and 6 for the exponent:
-1 1/2 1/4 1/8 1/16 1/32 1/64 1/128 1/256 1/512 -32 16 8 4 2 1
0 1 1 0 1 0 1 0 0 0 0 0 0 1 0 0
Therefore the binary point must be moved four places to the right in the mantissa:
-1 1/2 1/4 1/8 1/16 1/32 1/64 1/128 1/256 1/512 -32 16 8 4 2 1
0 1 0 0 1 0 1 1 0 0 0 0 0 1 1 0
Answer:------------------------------------------------------------------------------------------------------
© 2007 Richard Jones, PO BOX 246, Cambridge, New Zealand. See: http://www.ib-computing.com/ for more worksheets
Page: 7 of 15
Student name:
Now for a negative mantissa and exponent:
-1 1/2 1/4 1/8 1/16 1/32 1/64 1/128 1/256 1/512 -32 16 8 4 2 1
1 1 0 1 1 0 1 0 0 0 1 1 1 0 1 0
Stage 2 - shift the mantissa, the exponent is -6, so move the binary point 6 places to the left:
-1 1/2 1/4 1/8 1/16 1/32 1/64 1/128 1/256 1/512 -32 16 8 4 2 1
0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Answers:
1 --------------------------------------------------------------------------------------------------------
2 --------------------------------------------------------------------------------------------------------
3 --------------------------------------------------------------------------------------------------------
4 --------------------------------------------------------------------------------------------------------
In the second and third representations above, there are leading zeros following the binary point and
therefore less space to store any low value bits.
Notice that the first number 2 1/128 could not be stored with the higher exponents, the LSB of the
mantissa would be lost. In order to preserve the maximum possible accuracy, the numbers are
normalised (as for the decimal floating point number example given at the start of this section).
© 2007 Richard Jones, PO BOX 246, Cambridge, New Zealand. See: http://www.ib-computing.com/ for more worksheets
Page: 8 of 15
Student name:
Because we are using two’s complement binary fractions, normalising positive and negative numbers is
different. Positive is not so bad: the MSB (excluding the sign bit) must be a 1 (ie no trailing zeros
after the point).
Thus
0.000111100 would be expressed as 0.111100000
an exponent of -3.
For negative numbers there must be no leading 1’s to the left of the point, the MSB must be a zero
(which is the significant bit in two’s complement):
(Why? Leading 1’s in a two’s complement negative number may be ignored in just the same way that
leading zeros may be ignored in a positive number).
To confirm this, complement the mantissa and then shift it 5 places to the right. Write the binary
number resulting here:
Quick Quiz:
Which of the following decimal numbers link with which normalised binary numbers:
123 0110000000111110
0.1875 1000100000111111
-15/32 1011000100000110
-39.5 0111101100000111
Question: What happens if you allocate 5 bits to the mantissa and 8 bits to the exponent?
Student name:
What is the largest positive number that can be represented? The minimum positive number will
require the smallest positive mantissa (normalised, don’t forget) and the largest negative exponent.
Binary:......................................................................................
Decimal:...................................................................................
Binary:......................................................................................
Decimal:...................................................................................
Binary:......................................................................................
Decimal:...................................................................................
Binary:......................................................................................
Decimal:...................................................................................
(answers 124, 0.001953125, -128, -0.002075195313-ish)
• Complete accuracy of storage (as long as the number fits in the number of bits allocated).
• Large range is possible with enough bits.
• Overflow is easily recognised and trapped by programmers.
Floating point errors can also accumulate especially in long and complicated calculations leading to a
loss of precision in output.
© 2007 Richard Jones, PO BOX 246, Cambridge, New Zealand. See: http://www.ib-computing.com/ for more worksheets
Page: 10 of 15
Student name:
Errors
Consider the addition of floating point numbers:
If the numbers are smaller, or use fewer bits we may be simply able to convert floating point numbers
to a fixed point representation and then add them in the traditional way:
For example, using 12-bits (8 for a fractional two’s complement mantissa and 4 for the two’s
complement integer exponent) add the following two numbers:
010.01000
and
01111.000
first 0 1 0 . 0 1 0 0 0
second 0 1 1 1 1 . 0 0 0
result 1 0 0 0 1 . 0 1 0 0 0
To normalise we move the decimal point 5 places to the left, giving an exponent of 5, so the answer is:
0.1000101 0101
© 2007 Richard Jones, PO BOX 246, Cambridge, New Zealand. See: http://www.ib-computing.com/ for more worksheets
Page: 11 of 15
Student name:
For example, using a 10-bit mantissa with a 6-bit exponent, where it is very difficult to convert
numbers to a fractional representation:
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
-1 1/2 1/4 1/8 1/16 1/32 1/64 1/128 1/256 1/512 -32 16 8 4 2 1
1 0 1 1 0 1 1 0 1 1 0 0 1 1 1 1
Just so we can check the correct result later, let’s convert this binary value to its decimal equivalent.
- 0.100100101
Expressed as a decimal this is -0.5722656 approximately, as a power of 2 it is -293 x 2-9, either way we
have to multiply it by the exponent of (001111) 215 (32768).
© 2007 Richard Jones, PO BOX 246, Cambridge, New Zealand. See: http://www.ib-computing.com/ for more worksheets
Page: 12 of 15
Student name:
From here we can work out that its decimal value is:
Suppose we wish to add 294 to this number, that would be (1/2 + 1/16 + 1/128 + 1/256) x 29
approximately or
0100100110 001001
in our representation. Now we have 001111 for the first exponent and 001010 for the second.
So, for the smaller, we have to repeatedly shift the mantissa one place to the right and increase the
exponent by one until it is equal to the larger:
And the exponents of the two numbers are now equal. Notice, however, that our original value of 294
has been altered to 256 (32766/128) by this process, ie accuracy has been lost.
In this case, accuracy has been lost as the final bits “fell off” the end of our mantissa – a truncation
error.
1011011011
+ 0000000100
1011011111 exp 001111
The true answer (-18752 + 294) is –18458 so the process has resulted in some error.
© 2007 Richard Jones, PO BOX 246, Cambridge, New Zealand. See: http://www.ib-computing.com/ for more worksheets
Page: 13 of 15
Student name:
In real systems these errors are reduced by allocating more bits to the storage of floating point numbers.
For example, in Java, 4 bytes (32 bits) are used to store float primitives and 8 bytes are used to store
double primitives.
To improve accuracy, intermediate calculations will use more bits than the final stored result (this
would help to avoid the error in the calculation shown above).
If the result is too small to be stored (after division or subtraction. for example) in normalised form
then the error condition is known as underflow. Most systems will simply use zero to represent this
condition.
As we have seen truncation errors occur when bits are “chopped off” from the end of a number in
some process such as adjusting the mantissa of a number to be processed.
© 2007 Richard Jones, PO BOX 246, Cambridge, New Zealand. See: http://www.ib-computing.com/ for more worksheets
Page: 14 of 15
Student name:
Sample Questions
A control system is being designed for a washing machine using a 16-bit register to store data about the
state of the machine, such as whether the door is open or closed, the level of water in the machine and
the temperature selected for the wash (an integer).
[3 marks]
2. If the temperature can only be set at 5 degree intervals, from 25 to 75, how many bits are
required to store the settings?
………………………………………………………………………………………......
[1 mark]
3. In another control system 9 bits are available to store a temperature range of –90 to plus 90
degrees. Show how this could be done using a floating point binary representation while
maintaining the maximum possible precision.
[4 marks]
4. If the 9-bits were allocated to storing an integer in two’s complement form, what would be the
minimum and maximum value that could be stored (you may give your answer as a power of
2).
..........................................................................................................................................
..........................................................................................................................................
[2 marks]
Assume there exists a hypothetical decimal computer that stores numbers in a word. A word which
stores an integer value can hold eight symbols, one sign (+ or -) in the leftmost position and seven
decimal digits. A word which stores a floating-point number can hold eight symbols but the word is
divided into two parts. One part can hold three symbols. It is called the exponent and is composed of a
sign and two decimal digits. It is located in the leftmost (high order) three positions.
The second part which can hold five symbols is the mantissa. This part is composed of a sign and four
decimal digits normalised to the most significant digit and is located in the rightmost (low order) five
© 2007 Richard Jones, PO BOX 246, Cambridge, New Zealand. See: http://www.ib-computing.com/ for more worksheets
Page: 15 of 15
Student name:
positions. The digits of the mantissa are normalised as indicated in the floating-point example (-
2.73479) below.
-672 is represented as - 0 0 0 0 6 7 2
-2.73479 is represented as + 0 1 - 2 7 3 6
(a) What is the smallest integer and the largest integer that can be stored?
[2 marks]
(b) What is the largest floating-point number that can be stored? What is the smallest floating-point
number greater than 0 that can be stored. In both cases give the answer as a power of 10.
[2 marks]
(c) ..........................................................................................................................................
Given the range of available real values in this computer, copy and complete the real number line
below. Indicate the range of negative and positive real values, underflow and overflow, by
means of labels.
[8 marks]
© 2007 Richard Jones, PO BOX 246, Cambridge, New Zealand. See: http://www.ib-computing.com/ for more worksheets