Programable Logic Controller questions. PLCS
MODULE TITLE: PROGRAMMABLE LOGIC CONTROLLERS
TOPIC TITLE: INTRODUCTORY BASIC THEORY
LESSON 2: NUMBER SYSTEMS
PLC - 1 - 2
© Teesside University 2011
Published by Teesside University Open Learning (Engineering)
School of Science & Engineering
Teesside University
Tees Valley, UK
TS1 3BA
+44 (0)1642 342740
All rights reserved. No part of this publication may be reproduced, stored in a
retrieval system, or transmitted, in any form or by any means, electronic, mechanical,
photocopying, recording or otherwise without the prior permission
of the Copyright owner.
This book is sold subject to the condition that it shall not, by way of trade or
otherwise, be lent, re-sold, hired out or otherwise circulated without the publisher's
prior consent in any form of binding or cover other than that in which it is
published and without a similar condition including this
condition being imposed on the subsequent purchaser.
________________________________________________________________________________________
INTRODUCTION ________________________________________________________________________________________
In this lesson we shall learn what we mean by a number ‘base’ and consider
number systems other than the one that we use in everyday life, the denary or
decimal number system. Central processing units of computers require digital
signals, i.e. two-state signals, which make the binary system of numbers
convenient for processing taks. The binary numbering system, however, is
unwieldly and the ‘hexadecimal’ system is a much more compact system and
more convenient to use for tasks associated with computers. As programmable
logic controllers are essentially computers, it will benefit us to understand
these different numbering systems.
________________________________________________________________________________________
YOUR AIMS ________________________________________________________________________________________
On completing this lesson you should be able to:
• explain what is meant by positional notation
• define the numer bases denary, binary and hex
• convert between one number base and another
• perform binary addition and subtraction
• understand what is meant by the complement of a number
• understand binary addition involving negative binary numbers
• understand BCD systems of binary coding.
1
Teesside University Open Learning (Engineering)
© Teesside University 2011
________________________________________________________________________________________
NUMBER SYSTEMS ________________________________________________________________________________________
We are used to counting things in batches of ten. That is to say, we normally
work to a base of ten. Habitual use of this number base may, mistakenly, lead
us to the conclusion that the number ten has some special property not to be
found in other numbers. This is not so and the choice of ten as our ‘everyday’
number base is due only to the biological fact that we have a total of ten digits
on our two hands. The number ten has no special mathematical significance.
The numbering system to a base of ten is called the DECIMAL or DENARY
system. In the denary system the symbols 0, 1, 2 ......9 are used to represent
zero, one, two ......nine objects. If we count beyond nine then POSITIONAL
NOTATION is used, so that ten objects are represented by the two symbols
‘10’, eleven by ‘11’, etc. In positional notation the numerical weighting of
each digit increases by a factor of ten each time we move one position to the
left. This gives us the ‘units, tens, hundreds and thousands’ of the denary
system. Thus the symbols ‘1011’ in denary are taken to represent:-
[(1 × 103) + (0 × 102) + (1 × 101) + (1 × 100)] Thousands Hundreds Tens Units
(Remember, any number raised to the power of zero is equal to 1.
Thus 100 = 1, 80 = 1, x0 = 1)
Although what we have outlined above might seem obvious, it is important to
remember that not all numbering systems have used the same symbols and
notation as used in denary. Consider, for example, Roman numerals which use
the symbols I, V, X, L, C, D and M for the numbers one, five, ten, fifty,
one hundred, five hundred and one thousand respectively. Thus 1999 is
represented by MCMXCIX!
2
Teesside University Open Learning (Engineering)
© Teesside University 2011
We can also have denary numbers with a decimal point, such as
So, 101.101 represents (1 × 102) + (0 × 101) + (1 × 100) + (1 × 10–1) + (0 × 10–2) + (1 × 10–3)
OTHER NUMBER BASES
When we wish to work to bases other than ten, then it makes sense to use, as
far as possible, the same symbols as used in denary and also to employ
positional notation. Thus, if working to a base of 8 (OCTAL) then we will use
the symbols 0 – 7 and the numerical weighting of each digit will increase by a
factor of eight for each move to the left.
Thus the number ‘1011’ in OCTAL represents
[(1 × 83) + (0 × 82) + (1 × 81) + (1 × 80)].
You may care to check that this is equivalent to the denary number ‘521’.
(1 × 83 = 512, 0 × 82 = 0, 1 × 81 = 8, 1 × 80 = 1.) To avoid confusion about which number base is being used, a subscript may be added to the
number to indicate its base. Thus:
10118 = 52110 base of 8 base of 10
(Note that the subscript is always given in denary. The notation
101110 = 52112 (giving the bases in octal) really could cause confusion!)
0 1 1
10 1
10 10
0 01 1
100 1
10 10
1 1
2
.
.
–= = =
= = =and –– ,2 etc.
3
Teesside University Open Learning (Engineering)
© Teesside University 2011
BINARY NUMBERS
Digital computers use two-state devices (the states represented by ‘0’ and ‘1’)
to store and process data. The binary counting system, using a base of two,
must therefore be used. In binary, only the symbols ‘0’ and ‘1’ are required
and each position in a binary number represents a power of two.
Thus the binary number ‘1011’ represents:
[(1 × 23) + (0 × 22) + (1 × 21) + (1 × 20)] giving 10112 = 1110.
Similarly, the binary number ‘11000111’ represents:
[(1 × 27) + (1 × 26) + (0 × 25) + (0 × 24) + (0 × 23) + (1 × 22) + (1 × 21) + (1 × 20)]
= 128 + 64 + 4 + 2 + 1 = 199 in denary
i.e. 110001112 = 19910
Most significant bit Least significant bit
27
128
0
26
64
0
25
32
0
24
16
1
23
8
1
22
4
0
21
2
0
20
1
0
4
Teesside University Open Learning (Engineering)
© Teesside University 2011
BINAY NUMBERS WITH ‘DECIMAL’ POINTS
As with decimal numbers, fractional binary numbers can be represented with a
binary point. For example:
and
As we can have only 0 or 1 in binary then
0.5 in decimal becomes 0.1 binary
3.5 decimal would become 11.1 in binary
3.75 decimal would become 11.11 (i.e. 3 + 1/2 + 1/4).
These rules also apply to other bases.
binary 0.1 = 1 2
=
binary 0.01 = 1
2 = ,
2
2
2
1
2
–
– eetc
5
Teesside University Open Learning (Engineering)
© Teesside University 2011
CONVERTING BINARY NUMBERS TO DECIMAL
Example 1
Convert 1100011102 to denary.
Solution
i.e. 1100011102 = 39810
It is immediately apparent that whilst the binary system requires only two
symbols (and is therefore ideally suited to use with two-state devices) its
number length becomes very cumbersome when representing large numbers.
For example, ‘one thousand’ in binary is ‘1111101000’. To avoid having to
handle very long binary numbers, the number is often split into groups of four
bits – each pattern of four bits having its own symbol. This gives rise to a new
number system – the HEXADECIMAL system (or simply ‘HEX’).
110001110 1 2 1 2 0 2 0 2
0
2 8 7 6 5= ×( ) + ×( ) + ×( ) + ×( )⎡⎣
+ × 22 1 2 1 2 1 2
0 2
256 128
4 3 2 1
0
( ) + ×( ) + ×( ) + ×( )
+ ×( )⎤⎦ = + ++ + + =8 4 2 398.
6
Teesside University Open Learning (Engineering)
© Teesside University 2011
HEXADECIMAL NUMBER SYSTEM
Although a computer works in binary, the data may be entered into the
machine using ‘HEX’, as HEX is more concise and easier for us to handle.
The machine will then convert the HEX to binary. The HEX system uses a
base of sixteen – but what symbols shall we use in a system that has more than
ten digits? We could invent a completely new set of symbols but these would
have to be memorised; it is best that we stick with the symbols we are already
familiar with. In fact, in HEX, the numerical symbols 0 – 9 and the
alphabetical symbols A – F have been chosen. The table below and overleaf
gives the HEX representation of the denary numbers 0 to 32. The binary
equivalents are also given (expressed as 8 bit numbers).
DENARY HEX BINARY
0 0 0000 0000
1 1 0000 0001
2 2 0000 0010
3 3 0000 0011
4 4 0000 0100
5 5 0000 0101
6 6 0000 0110
7 7 0000 0111
8 8 0000 1000
9 9 0000 1001
10 A 0000 1010
11 B 0000 1011
12 C 0000 1100
13 D 0000 1101
14 E 0000 1110
15 F 0000 1111
16 10 0001 0000
17 11 0001 0001
18 12 0001 0010
7
Teesside University Open Learning (Engineering)
© Teesside University 2011
19 13 0001 0011
20 14 0001 0100
21 15 0001 0101
22 16 0001 0110
23 17 0001 0111
24 18 0001 1000
25 19 0001 1001
26 1A 0001 1010
27 1B 0001 1011
28 1C 0001 1100
29 1D 0001 1101
30 1E 0001 1110
31 1F 0001 1111
32 20 0010 0000
• • •
• • •
• • •
48 30 0011 0000
• • •
• • •
• • •
96 60 0110 0000
• • •
• • •
• • •
255 FF 1111 1111
This ‘alphanumeric’ system of symbols in a counting system may seem a little
strange at first, but with a little practice you will soon get used to it! Note that
in the HEX system, positional notation is still used as illustrated by the
following example.
8
Teesside University Open Learning (Engineering)
© Teesside University 2011
CONVERSION FROM HEX TO DENARY
Example 2
Convert the HEX number 7A4E into denary.
Solution
From the preceding table A = 1010 and E = 1410. By means of positional
notation we can write:
7A4E = [(7 × 163) + (10 × 162) + (4 × 161) + (14 × 160) = 31310
163 162 161 160
Should there be any risk of confusion over which number base is being used
then the convention is that the HEX number is followed by a capital ‘H’.
Thus 13H = 1910, 21H = 3310, 1000H = 409610.
In this module we shall only be using binary, denary or HEX systems and
therefore any number with alphabetical symbols will automatically be HEX.
OTHER BASE CONVERSIONS
Hex to Binary
This is particularly easy as the HEX base of 16 is equal to 24. All we need do
is convert each HEX symbol into its four bit binary equivalent using our
conversion table.
9
Teesside University Open Learning (Engineering)
© Teesside University 2011
Example 3
Convert ABCD to binary.
Solution
A = 1010, B = 1011, C = 1100 and D = 1101
Thus ABCD = 1010 1011 1100 11012
It would be a useful exercise for you to check this result by converting both
numbers independently to denary.
Binary to Hex
This is simply the reverse of the above process. The binary number is split
into groups of four starting with the least significant bit (LSB):
Example 4
Convert 1010 0000 1111 00112 to HEX
Solution
1010 0000 1111 0011 = A0F3
| | | |
A 0 F 3
10
Teesside University Open Learning (Engineering)
© Teesside University 2011
________________________________________________________________________________________
ADDITION OF BINARY NUMBERS ________________________________________________________________________________________
In principle, binary numbers are added in exactly the same way as in denary
addition – but remember we are working to a base of two! This means that
(1 + 1) will generate a ‘carry’. The rules of binary addition are summarised
below:
0 0 1 1
+ 0 + 1 + 0 + 1
0 1 1 0 carry 1
You may find it worthwhile to write these rules down several times so that you
remember them.
Then study the following examples.
Example 5
Add the binary numbers 1010 and 0011.
Solution
1 0 1 0
+ 0 0 1 1
1 1 0 1
carry
The bits in each column are added together and any ‘carry bit’ generated is
added into the column on the left.
∪
11
Teesside University Open Learning (Engineering)
© Teesside University 2011
Example 6
Add the binary numbers 1011 and 0011.
Solution
* 1 0 1 1
+ 0 0 1 1
1 1 1 0
carry
In this example the column marked with an asterisk represents the addition
(1 + 1 + 1) = 11. This means that 1 is entered into this column and one is
carried over to the next most significant column.
∪∪
12
Teesside University Open Learning (Engineering)
© Teesside University 2011
________________________________________________________________________________________
BINARY SUBTRACTION ________________________________________________________________________________________
There is no theoretical reason why binary numbers cannot be subtracted using
the same rules as for denary subtraction, i.e. starting with the least significant
column we subtract one bit from another. The rules for binary subtraction will
then be:
0 1 1 0
– 0 – 0 – 1 – 1 borrow 1 from left hand
0 1 0 1 column
In the fourth rule, ‘1’ from ‘0’ ‘won’t go’ and we have to borrow a bit from the
adjacent column. This now forms the equivalent of (2 – 1) = 1.
Example 7
Subtract 0111 from 1111.
Solution
1 1 1 1
– 0 1 1 1
1 0 0 0
Note that the answer can be checked by adding the result to the number
subtracted.
0 1 1 1
+ 1 0 0 0
1 1 1 1
13
Teesside University Open Learning (Engineering)
© Teesside University 2011
Example 8
Subtract 0011 from 1101.
Solution
1 1 0 1 In column 2 we have to borrow
– 0 0 1 1 from column 3.
1 0 1 0
borrow
Good fun as it might be(!), this is as far as we shall go in binary subtraction.
The reason for this is that computers do not perform subtraction in this way.
There are two reasons why they do not:
(a) Binary addition is carried out in the arithmetic logic unit (ALU) built into
the CPU. The addition is done by special ‘adder ’ hardware. If
subtraction were to be done in the ‘conventional’ way then ‘subtractor’
hardware would also be required.
(b) Subtraction can give rise to negative results. Thus we need a means of
representing positive and negative numbers. Conventionally we do this
by using the symbols ‘+’ and ‘–’. We must remember, however, that a
binary machine will only recognise two symbols (namely ‘0’ and ‘1’).
We cannot therefore use extra symbols to denote a number’s sign.
∪
14
Teesside University Open Learning (Engineering)
© Teesside University 2011
________________________________________________________________________________________
REPRESENTATION OF NEGATIVE NUMBERS ________________________________________________________________________________________
Two methods will be considered. The first of these is widely used in data
communications systems.
(a) THE SIGN-MAGNITUDE METHOD
The magnitude of a number is its value, ignoring its sign. Thus the magnitude
of ‘+3’ is ‘3’ and of ‘–8’ is ‘8’.
The magnitude of ‘minus one hundred’ is equal to the magnitude of ‘plus one
hundred’.
In the sign magnitude method, the sign of a binary number is given by the
MSB. The remaining bits give the magnitude of the number. If positive
numbers are represented by the MSB being set to ‘0’, then negative numbers
are represented by the MSB being set to ‘1’. To illustrate the idea, the binary
equivalents of –5 to +5 are listed below (using 1 byte numbers):
SIGN BIT MAGNITUDE ___________________________
–5 1 0000101 ___________________________
–4 1 0000100 ___________________________
–3 1 0000011 ___________________________
–2 1 0000010 ___________________________
–1 1 0000001 ___________________________
15
Teesside University Open Learning (Engineering)
© Teesside University 2011
–0 1 0000000 ___________________________
+0 0 0000000 ___________________________
+1 0 0000001 ___________________________
+2 0 0000010 ___________________________
+3 0 0000011 ___________________________
+4 0 0000100 ___________________________
+5 0 0000101 ___________________________
As a matter of curiosity, note that this method gives two zeros, 10000000
(minus zero) and 00000000 (plus zero)! This, however, does not cause any
problems.
Also, there is, as a matter of fact, no hard and fast rule as to whether a ‘1’ or a
‘0’ is used to represent a positive number. Which convention is adopted will
depend upon the particular system.
The advantage of this method is the simplicity with which it allows the
magnitude of the number to be quickly established. A serious disadvantage
though, is that the method cannot easily be used for binary arithmetic.
Consider the following examples.
16
Teesside University Open Learning (Engineering)
© Teesside University 2011
Example 9
Add (+3) to (–3) using binary signed numbers.
Solution
+3 = 00000011 and –3 = 10000011
0 0 0 0 0 0 1 1
+ 1 0 0 0 0 0 1 1
1 0 0 0 0 1 1 0
Clearly the result should have been zero ((+3) + (–3) = 0) but the addition
has come up with –6.
Example 10
Add (–5) to (+2).
Solution
–5 = 10000101 and +2 = 00000010
1 0 0 0 0 1 0 1
0 0 0 0 0 0 1 0
1 0 0 0 0 1 1 1
This time the answer is –7 and not –3 as it should be.
This system is clearly no good at arimethic. Nevertheless it does have the
value of simplicity and is used in systems where the data does not have to be
processed arithmetically.
17
Teesside University Open Learning (Engineering)
© Teesside University 2011
(b) THE TWO’S COMPLEMENT METHOD
The ‘one’s complement’ of a binary number is obtained by changing all the
‘1’s to ‘0’s and vice versa. (This process is known as INVERTING the
number.) A few examples of binary numbers and their ‘one’s complements’
are given below.
NUMBER ONE’S COMPLEMENT
0 0 0 0 1 1 1 1
1 0 1 0 0 1 0 1
0 0 1 1 1 1 0 0
1 1 1 1 0 0 0 0
The two’s complement of a binary number is formed by taking the one’s
complement and adding 1 to it.
Example 11
Find the ‘two’s complement’ of 14. Express the answer as an 8 bit number.
Solution
1410 = 00001110
‘One’s complement’ = 11110001
‘Two’s complement’ = (‘one’s complement’ + 1)
= (11110001 + 1)
= 11110010
18
Teesside University Open Learning (Engineering)
© Teesside University 2011
The ‘two’s complement’ form can be used to represent the negative of a binary
number which can be used in arithmetic operations. It may be a bit tricky for
us to use and recognise the ‘two’s complement’ form, but relatively simple
arithmetic circuits in the CPU are quite at home with them. To convince
yourself that the ‘two’s complement’ representation of binary numbers works,
study the following examples.
Example 12
Use the ‘addition of the two’s complement’ method perform the operation
(25 – 14).
Solution
(25 – 14) may be rewritten as (25 + (–14)).
The procedure is to represent (–14) in two’s complement form and then add
this to 25.
2510 = 0 0 0 1 1 0 0 1
add (–1410) = 1 1 1 1 0 0 1 0
[1] 0 0 0 0 1 0 1 1
↑ carry
As we are working in 8 bit numbers, the ‘carry one’ is ignored. The result is,
therefore, 000010112 = 1110, the correct answer.
19
Teesside University Open Learning (Engineering)
© Teesside University 2011
Example 13
Use the ‘two’s complement’ representation show that 25 – 25 = 0.
Solution
25 = 0 0 0 1 1 0 0 1
add (–25) = 1 1 1 0 0 1 1 1
[1] 0 0 0 0 0 0 0 0
↑ carry
Again, the carry bit is ignored and a zero result is obtained.
Example 14
Use the ‘two’s complement’ representation evaluate (14 – 25).
Solution
14 = 0 0 0 0 1 1 1 0
add (–25) = 1 1 1 0 0 1 1 1
1 1 1 1 0 1 0 1
This answer highlights one of the difficulties of the ‘two’s complement’ form.
In the ‘complement’ system all negative numbers have a MSB of 1, so we
know our result is negative, but the magnitude of the number is by no means
obvious!
20
Teesside University Open Learning (Engineering)
© Teesside University 2011
Before attempting to evaluate, in denary, the solution to example 14, let us
investigate our new system a little more closely. Initially, for simplicity, we
shall use 4 bit signed numbers. This gives a numbering range of (–8) to (+7)
as indicated on the ‘number line’ shown below.
The clue to evaluating the magnitude of a negative number represented in
‘two’s complement’ form is to note that, for example, (–3) = (–8 + 5).
Looking at the number here we see that (–3) = 1101. Thus the ‘two’s
complement’ form can be regarded as consisting of a mixture of two numbers:
(a) a negative number represented by the MSB and equal to (–2n–1) where n
is the bit length of the number
(b) a positive number evaluated from the remaining bits.
The result is, of course, obtained by adding the two numbers together.
[Those students who have studied logarithms will perhaps recall that a
similar technique was used to represent negative logarithms by means of
‘bar’ numbers. Thus represents (–1 + 0.1234).]1 1234.
NEGATIVE NUMBER POSITIVE NUMBER
(–2n–1) = (–23) = –8 (22 + 20) = 5
1 1 0 1
–8 –7 –6 –5 –4 –3 –2 –1 0 +1 +2 +3 +4 +5 +6 +7
1000
(–8+0)
1001
(–8+1)
1010
(–8+2)
1011
(–8+3)
1100
(–8+4)
1101
(–8+5)
1110
(–8+6)
1111
(–8+7)
0000 0001 0010 0011 0100 0101 0110 0111
DENARY
BINARY
21
Teesside University Open Learning (Engineering)
© Teesside University 2011
We are now in a position to return to Example 14. The result is an 8 bit signed
number and thus the number range will be from (–128) to (+127). This range
was evaluated using the formulae:
(a) lower limit = (–2n–1) giving –128 for n = 8
(b) upper limit = (2n–1 – 1) giving +127 for n = 8
Splitting (11110101) into two parts yields:
(–28–1) = –128 (26 + 25 + 24 + 22 + 20) = 117
The answer, in denary is, therefore, (–128 + 117) = –11.
Alternative method
An alternative way to find the 2’s complement, instead of firstly finding the 1’s
complement is:
(a) Write down the binary number, starting with the LSB, up to and including
the first ‘1’.
(b) Invert the remaining bits.
The following example shows how this is done.
Example 15
Write down the ‘two’s complement’ of 01110010.
11110101 � �� ��
22
Teesside University Open Learning (Engineering)
© Teesside University 2011
Solution
invert these bits to give 'up to and including the first '1" gives
'1 0 0 0 1 1' '1 0'
Thus ‘two’s complement of 01110010 is 10001110.
Example 16
Perform the arithmetic operation (17 – 97) in binary. Express your answer in
‘two’s complement’ form and then convert it to denary. Work in 8 bit
numbers.
Solution
17 = 00010001
97 = 01100001
∴ (–97) = 10011111 (in ‘two’s complement)
Adding (–97) to (+17):
0 0 0 1 0 0 0 1
+ 1 0 0 1 1 1 1 1
1 0 1 1 0 0 0 0
Thus in ‘two’s complement’ the result is 10110000 (which is clearly negative
as the MSB is set to ‘1’).
01110 010 ��� �� �
23
Teesside University Open Learning (Engineering)
© Teesside University 2011
Converting this to denary:
10110000 = (–28–1) + (25 + 24)
= (–128 + 48)
= –80
This technique can be used in reverse for easy identification of the magnitude
of negative binary numbers if the binary number is known to be negative.
Consider the result to Example 16. Applying the technique in reverse we:
(a) write down the number up to and including the first ‘1’ (yielding – 10000)
(b) invert all the other bits (giving ‘01010000’).
The magnitude is thus ‘01010000’ = 80
As we know we are dealing with a negative number the answer is –80.
Which method you use (the more formal ‘[–2n -1 + (positive number)]’ or the
‘trick’) is entirely up to you – there is little to choose between them in terms of
speed of conversion.
24
Teesside University Open Learning (Engineering)
© Teesside University 2011
________________________________________________________________________________________
BINARY CODED DECIMAL ________________________________________________________________________________________
This is a system of representing each denary digit by its binary equivalent.
The system is based on a weighting system of 8421. This simply means that
each decimal digit is represented by 4 binary digits and that the binary LSB is
represented by 20 (i.e. 1) through to the MSB which is 23 (i.e. 8). The
weighting is therefore 8421.
The number 457 will be represented, therefore, with four binary digits for each
denary or decimal number as:
0100 0101 0111
4 5 7
BCD is commonly used with input and output devices. A thumbwheel switch is
one example of an input device that uses BCD. The binary numbers are broken
into groups of four bits, each group representing a decimal equivalent. A four-
digit thumbwell switch, like the one shown here, would control 16 (4 × 4) PLC inputs.
Decimal numbers
0 1 2 3 4 5 6 7 8 9
0 2 0 5
0000 0010 0000 0101
BCD numbers
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
25
Teesside University Open Learning (Engineering)
© Teesside University 2011
BINARY CODED DECIMAL (BCD) AND THE TEN'S COMPLEMENT
Two's complement notation is used to represent negative binary numbers. For
example, in two's complement 1111 1111 represents minus one. The
complement of a number, however, is not peculiar to the binary system.
The complement notation can be used to represent negative numbers in any
number base. Here we shall consider the representation of signed BCD
numbers using the ten's complement.
Quickly, mentally subtract 7 from 6 and then compare your result with that
below :
6
– 7
(1) 9
This result, at first sight, might seem a little strange, for, no doubt, you
expected an answer of – 1! But how did you arrive at your answer? I suspect
you looked at the two numbers, decided that 7 was the larger and then
subtracted 6 from it. You then called the result negative. This technique is fine
for humans but it does require a little intuition, a property for which computers
are not particularly renowned. In subtracting two numbers they will perform
the operation in the order in which the numbers are given. To carry out the
above calculation, therefore, a computer will borrow when attempting to
subtract 7 from 6 so that the calculation becomes 7 from 16 to give 9. The 1
that has been borrowed is shown in brackets in the working above. If the
machine had a two-digit display, then 99 would be displayed.
As a further illustration imagine an odometer (‘mileometer’) set to ...0006 miles
and then wound back 7 places. The resulting display would be ...9999, i.e. ‘9’
followed by a long succession of nines. This is, in fact, the ten’s complement
representation of minus one.
26
Teesside University Open Learning (Engineering)
© Teesside University 2011
The number line below shows the numbers –10 to +9, the negative numbers
being represented by their two-digit, ten’s complement form. Note that in such
representation all negative numbers are prefixed by a nine and all positive
numbers by a zero.
When working in BCD, negative numbers will be represented by the computer
in their ten's complement form. So for example, 17 – 39 will be displayed as
78 on a two-digit display. The ten’s complement of a single decimal digit can
be found by subtracting it from ten.
(where N is the number and its ten’s
complement)
i.e. a denary number and its ten’s complement
equals 10. Generally, in any base, a number
plus its complement equals the base.
However, it is easier to program a machine to first form the nine’s complement
of the number and then add 1 to the result. (The nine’s complement is formed
by subtracting the number from 9). This process avoids having to ‘borrow’.
The above equation can be re-written as:
But (9 – N) is the nine’s complement of N.
N N
N N
10
10
9 1
9 1
′
′
= +( )
∴ = ( ) +
–
–
or N N10 10 ′ + =
N10 ′N N10 10
′ = –
Ten's complement Signed numbers
90 91 92 93 94 95 96 97 98 99 00 01 02 03 04 05 06 07 08 09
–10 –9 –8 –7 –6 –5 –4 –3 –2 –1 0 +1 +2 +3 +4 +5 +6 +7 +8 +9
27
Teesside University Open Learning (Engineering)
© Teesside University 2011
Hence
This process should be compared with the formation of the two’s complement
in natural binary. You'll recall that to form the two’s complement of a binary
number, the number is first complemented by inverting the bits and then
adding 1. The table below gives the nine’s and ten’s complement in BCD of
the numbers 0 to 9.
Decimal Number BCD nine’s complement ten’s complement
0 0000 1001 1010
1 0001 1000 1001
2 0010 0111 1000
3 0011 0110 0111
4 0100 0101 0110
5 0101 0100 0101
6 0110 0011 0100
7 0111 0010 0011
8 1000 0001 0010
9 1001 0000 0001
What is the result of adding a BCD number to its ten’s complement?
....................................................................................................................................................
....................................................................................................................................................
__________________________________________________________________________
The result is always ten (1010).
where represents the nine's complementN9 ′( )).
N N10 9 1 ′ ′= +
28
Teesside University Open Learning (Engineering)
© Teesside University 2011
Can the nine's complement be formed by bit inversion?
....................................................................................................................................................
....................................................................................................................................................
....................................................................................................................................................
....................................................................................................................................................
__________________________________________________________________________
No, it is one of the disadvantages of this particular BCD code that the nine’s complement
cannot be formed by simple bit inversion. This does make arithmetic operations more
difficult.
We shall now look at examples of other BCD codes in which bit inversion can
be used to form the nine’s complement. Codes which possess this property are
said to be self-complementary.
29
Teesside University Open Learning (Engineering)
© Teesside University 2011
________________________________________________________________________________________
OTHER BCD CODES ________________________________________________________________________________________
The BCD above is known as 8-4-2-1 BCD, the 8, 4, 2 and 1 referring to the
weighting given to each bit position. Various other BCD codes have been
devised, usually for processors which do not have decimal adjust instructions,
to make arithmetic operations easier. We shall briefly acquaint ourselves with
one such code.
2-4-2-1 BCD
The most significant bit of 2-4-2-1 BCD code has a weighting of only two,
instead of the customary eight. This is done so that the code automatically
compensates for the difference of six between the HEX and decimal number
bases.
The code is listed below. Note that a disadvantage of 2-4-2-1 BCD is that
some of the numbers have two code words. For example, five can be
represented by 1011 or by 0101.
Decimal (2-4-2-1)BCD nine’s complement ten’s complement
0 0000 1111 0000
1 0001 1110 1111
2 0010 (1000) 1101 1110
3 0011 (1001) 1100 1101
4 0100 (1010) 1011 1100
5 1011 (0101) 0100 0101
6 1100 (0110) 0100 0100
7 1101 (0111) 0011 0011
8 1110 0001 0010
9 1111 0000 0001
30
Teesside University Open Learning (Engineering)
© Teesside University 2011
The code is self-complementary, which means that the ten’s complement can
be formed by an ALU (Arithmetic and Logic Unit) designed to process natural
binary, that is by bit inversion and adding one to the result.
An example will show how 2-4-2-1 BCD works.
Example 17
Add eight to nine using 2-4-2-1 BCD.
Solution
decimal 2-4-2-1 BCD
8 1110
add 9 1111
17 1 1101
The 1 carried over in the BCD sum gives the correct result of seventeen
without the need for any adjustment.
That completes our look at number systems. Now try the Self-Assessment
Questions that follow to test your understanding of the lesson.
31
Teesside University Open Learning (Engineering)
© Teesside University 2011
________________________________________________________________________________________
SELF-ASSESSMENT QUESTIONS ________________________________________________________________________________________
1. Convert the following binary numbers to denary:
(a) 100000
(b) 1010101
(c) 000101
2. Convert the following binary numbers to HEX:
(a) 1010
(b) 11101001
(c) 1011100001111111
3. Convert the following HEX numbers to binary:
(a) A0
(b) C2
(c) BEF
4. Find the ‘two’s complement’ of 25.
5. Add seven to eight using 2-4-2-1 BCD.
32
Teesside University Open Learning (Engineering)
© Teesside University 2011
________________________________________________________________________________________
NOTES ________________________________________________________________________________________
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
33
Teesside University Open Learning (Engineering)
© Teesside University 2011
________________________________________________________________________________________
ANSWERS TO SELF-ASSESSMENT QUESTIONS ________________________________________________________________________________________
1. (a)
(b)
(c)
2. (a) 10102 = A
(b) 1110 10012 = E9
(c) 1011 1000 0111 11112 = B87F.
3. (a) A0 = 10100000
(b) C2 = 11000010
(c) BEF = 101111101111.
000101 1 2 0 2 1 2
5
2 2 1 0
10
= ×( ) + ×( ) + ×( )⎡⎣ ⎤⎦ =
1010101 1 2 0 2 1 2 0 2
1 2
2 6 5 4 3
2
= ×( ) + ×( )⎡⎣ + ×( ) + ×( )
+ ×(( ) + ×( ) + ×( )⎤⎦ = + + + + + +
=
0 2 1 2
64 0 16 0 4 0 1
85
1 0
10
100000 1 2 0 2 0 2 0 2
0 2
2 5 4 3 1
0
= ×( ) + ×( ) + ×( )⎡⎣ + ×( )
+ ×( ))⎤⎦ = 3210
34
Teesside University Open Learning (Engineering)
© Teesside University 2011
4.
5. Decimal 2-4-2-1 BCD
7 1101
8 1110
15 1 1011
25 00011001
11100
10 2=
∴ = 'two's complement' 1110 1 11100111+( ) =
35
Teesside University Open Learning (Engineering)
© Teesside University 2011
________________________________________________________________________________________
SUMMARY ________________________________________________________________________________________
We have encountered what is meant by a number system and should now be
aware of the following.
(a) Numbers consist of an ordered set of symbols, the numerical weighting of
each symbol increasing by a constant factor (called the BASE) as we
move from right to left. Such a numbering system uses POSITIONAL
NOTATION.
(b) In the denary (or decimal) system a base of TEN is used.
e.g. 3061 = [(3 × 103) + (0 × 102) + (6 × 101) + (1 × 100)]
(c) In the binary system a base of TWO is used.
e.g. 10112 = [(1 × 22) + (1 × 21) + (1 × 20)]
(d) In HEXADECIMAL a base of 16 is used.
e.g. ABC = [(10 × 162) + (11 × 161) + (12 × 160)]
(e) Conversion from binary and HEX to denary is by straight application of
positional notation.
We have also encountered other forms of binary coding. BCD is convenient
for representing decimal numbers in a binary form.
36
Teesside University Open Learning (Engineering)
© Teesside University 2011
<< /ASCII85EncodePages false /AllowTransparency false /AutoPositionEPSFiles true /AutoRotatePages /None /Binding /Left /CalGrayProfile (Dot Gain 20%) /CalRGBProfile (sRGB IEC61966-2.1) /CalCMYKProfile (U.S. Web Coated \050SWOP\051 v2) /sRGBProfile (sRGB IEC61966-2.1) /CannotEmbedFontPolicy /Error /CompatibilityLevel 1.4 /CompressObjects /Tags /CompressPages true /ConvertImagesToIndexed true /PassThroughJPEGImages true /CreateJDFFile false /CreateJobTicket false /DefaultRenderingIntent /Default /DetectBlends true /ColorConversionStrategy /LeaveColorUnchanged /DoThumbnails false /EmbedAllFonts true /EmbedJobOptions true /DSCReportingLevel 0 /SyntheticBoldness 1.00 /EmitDSCWarnings false /EndPage -1 /ImageMemory 1048576 /LockDistillerParams false /MaxSubsetPct 100 /Optimize true /OPM 1 /ParseDSCComments true /ParseDSCCommentsForDocInfo true /PreserveCopyPage true /PreserveEPSInfo true /PreserveHalftoneInfo false /PreserveOPIComments false /PreserveOverprintSettings true /StartPage 1 /SubsetFonts true /TransferFunctionInfo /Apply /UCRandBGInfo /Preserve /UsePrologue false /ColorSettingsFile () /AlwaysEmbed [ true ] /NeverEmbed [ true ] /AntiAliasColorImages false /DownsampleColorImages true /ColorImageDownsampleType /Bicubic /ColorImageResolution 300 /ColorImageDepth -1 /ColorImageDownsampleThreshold 1.50000 /EncodeColorImages true /ColorImageFilter /DCTEncode /AutoFilterColorImages true /ColorImageAutoFilterStrategy /JPEG /ColorACSImageDict << /QFactor 0.15 /HSamples [1 1 1 1] /VSamples [1 1 1 1] >> /ColorImageDict << /QFactor 0.15 /HSamples [1 1 1 1] /VSamples [1 1 1 1] >> /JPEG2000ColorACSImageDict << /TileWidth 256 /TileHeight 256 /Quality 30 >> /JPEG2000ColorImageDict << /TileWidth 256 /TileHeight 256 /Quality 30 >> /AntiAliasGrayImages false /DownsampleGrayImages true /GrayImageDownsampleType /Bicubic /GrayImageResolution 300 /GrayImageDepth -1 /GrayImageDownsampleThreshold 1.50000 /EncodeGrayImages true /GrayImageFilter /DCTEncode /AutoFilterGrayImages true /GrayImageAutoFilterStrategy /JPEG /GrayACSImageDict << /QFactor 0.15 /HSamples [1 1 1 1] /VSamples [1 1 1 1] >> /GrayImageDict << /QFactor 0.15 /HSamples [1 1 1 1] /VSamples [1 1 1 1] >> /JPEG2000GrayACSImageDict << /TileWidth 256 /TileHeight 256 /Quality 30 >> /JPEG2000GrayImageDict << /TileWidth 256 /TileHeight 256 /Quality 30 >> /AntiAliasMonoImages false /DownsampleMonoImages true /MonoImageDownsampleType /Bicubic /MonoImageResolution 1200 /MonoImageDepth -1 /MonoImageDownsampleThreshold 1.50000 /EncodeMonoImages true /MonoImageFilter /CCITTFaxEncode /MonoImageDict << /K -1 >> /AllowPSXObjects false /PDFX1aCheck false /PDFX3Check false /PDFXCompliantPDFOnly false /PDFXNoTrimBoxError true /PDFXTrimBoxToMediaBoxOffset [ 0.00000 0.00000 0.00000 0.00000 ] /PDFXSetBleedBoxToMediaBox true /PDFXBleedBoxToTrimBoxOffset [ 0.00000 0.00000 0.00000 0.00000 ] /PDFXOutputIntentProfile () /PDFXOutputCondition () /PDFXRegistryName (http://www.color.org) /PDFXTrapped /Unknown /Description << /ENU (Use these settings to create PDF documents with higher image resolution for high quality pre-press printing. The PDF documents can be opened with Acrobat and Reader 5.0 and later. These settings require font embedding.) /JPN <FEFF3053306e8a2d5b9a306f30019ad889e350cf5ea6753b50cf3092542b308030d730ea30d730ec30b9537052377528306e00200050004400460020658766f830924f5c62103059308b3068304d306b4f7f75283057307e305930023053306e8a2d5b9a30674f5c62103057305f00200050004400460020658766f8306f0020004100630072006f0062006100740020304a30883073002000520065006100640065007200200035002e003000204ee5964d30678868793a3067304d307e305930023053306e8a2d5b9a306b306f30d530a930f330c8306e57cb30818fbc307f304c5fc59808306730593002> /FRA <FEFF004f007000740069006f006e007300200070006f0075007200200063007200e900650072002000640065007300200064006f00630075006d0065006e00740073002000500044004600200064006f007400e900730020006400270075006e00650020007200e90073006f006c007500740069006f006e002000e9006c0065007600e9006500200070006f0075007200200075006e00650020007100750061006c0069007400e90020006400270069006d007000720065007300730069006f006e00200070007200e9007000720065007300730065002e0020005500740069006c006900730065007a0020004100630072006f0062006100740020006f00750020005200650061006400650072002c002000760065007200730069006f006e00200035002e00300020006f007500200075006c007400e9007200690065007500720065002c00200070006f007500720020006c006500730020006f00750076007200690072002e0020004c00270069006e0063006f00720070006f0072006100740069006f006e002000640065007300200070006f006c0069006300650073002000650073007400200072006500710075006900730065002e> /DEU <FEFF00560065007200770065006e00640065006e0020005300690065002000640069006500730065002000450069006e007300740065006c006c0075006e00670065006e0020007a0075006d002000450072007300740065006c006c0065006e00200076006f006e0020005000440046002d0044006f006b0075006d0065006e00740065006e0020006d00690074002000650069006e006500720020006800f60068006500720065006e002000420069006c0064006100750066006c00f600730075006e0067002c00200075006d002000650069006e00650020007100750061006c00690074006100740069007600200068006f006300680077006500720074006900670065002000410075007300670061006200650020006600fc0072002000640069006500200044007200750063006b0076006f0072007300740075006600650020007a0075002000650072007a00690065006c0065006e002e00200044006900650020005000440046002d0044006f006b0075006d0065006e007400650020006b00f6006e006e0065006e0020006d006900740020004100630072006f0062006100740020006f0064006500720020006d00690074002000640065006d002000520065006100640065007200200035002e003000200075006e00640020006800f600680065007200200067006500f600660066006e00650074002000770065007200640065006e002e00200042006500690020006400690065007300650072002000450069006e007300740065006c006c0075006e00670020006900730074002000650069006e00650020005300630068007200690066007400650069006e00620065007400740075006e00670020006500720066006f0072006400650072006c006900630068002e> /PTB <FEFF005500740069006c0069007a006500200065007300740061007300200063006f006e00660069006700750072006100e700f5006500730020007000610072006100200063007200690061007200200064006f00630075006d0065006e0074006f0073002000500044004600200063006f006d00200075006d00610020007200650073006f006c007500e700e3006f00200064006500200069006d006100670065006d0020007300750070006500720069006f0072002000700061007200610020006f006200740065007200200075006d00610020007100750061006c0069006400610064006500200064006500200069006d0070007200650073007300e3006f0020006d0065006c0068006f0072002e0020004f007300200064006f00630075006d0065006e0074006f0073002000500044004600200070006f00640065006d0020007300650072002000610062006500720074006f007300200063006f006d0020006f0020004100630072006f006200610074002c002000520065006100640065007200200035002e00300020006500200070006f00730074006500720069006f0072002e00200045007300740061007300200063006f006e00660069006700750072006100e700f50065007300200072006500710075006500720065006d00200069006e0063006f00720070006f0072006100e700e3006f00200064006500200066006f006e00740065002e> /DAN <FEFF004200720075006700200064006900730073006500200069006e0064007300740069006c006c0069006e006700650072002000740069006c0020006100740020006f0070007200650074007400650020005000440046002d0064006f006b0075006d0065006e0074006500720020006d006500640020006800f8006a006500720065002000620069006c006c00650064006f0070006c00f80073006e0069006e0067002000740069006c0020007000720065002d00700072006500730073002d007500640073006b007200690076006e0069006e0067002000690020006800f8006a0020006b00760061006c0069007400650074002e0020005000440046002d0064006f006b0075006d0065006e007400650072006e00650020006b0061006e002000e50062006e006500730020006d006500640020004100630072006f0062006100740020006f0067002000520065006100640065007200200035002e00300020006f00670020006e0079006500720065002e00200044006900730073006500200069006e0064007300740069006c006c0069006e0067006500720020006b007200e600760065007200200069006e0074006500670072006500720069006e006700200061006600200073006b007200690066007400740079007000650072002e> /NLD <FEFF004700650062007200750069006b002000640065007a006500200069006e007300740065006c006c0069006e00670065006e0020006f006d0020005000440046002d0064006f00630075006d0065006e00740065006e0020007400650020006d0061006b0065006e0020006d00650074002000650065006e00200068006f00670065002000610066006200650065006c00640069006e00670073007200650073006f006c007500740069006500200076006f006f0072002000610066006400720075006b006b0065006e0020006d0065007400200068006f006700650020006b00770061006c0069007400650069007400200069006e002000650065006e002000700072006500700072006500730073002d006f006d0067006500760069006e0067002e0020004400650020005000440046002d0064006f00630075006d0065006e00740065006e0020006b0075006e006e0065006e00200077006f007200640065006e002000670065006f00700065006e00640020006d006500740020004100630072006f00620061007400200065006e002000520065006100640065007200200035002e003000200065006e00200068006f006700650072002e002000420069006a002000640065007a006500200069006e007300740065006c006c0069006e00670020006d006f006500740065006e00200066006f006e007400730020007a0069006a006e00200069006e006700650073006c006f00740065006e002e> /ESP <FEFF0055007300650020006500730074006100730020006f007000630069006f006e006500730020007000610072006100200063007200650061007200200064006f00630075006d0065006e0074006f0073002000500044004600200063006f006e0020006d00610079006f00720020007200650073006f006c00750063006900f3006e00200064006500200069006d006100670065006e00200071007500650020007000650072006d006900740061006e0020006f006200740065006e0065007200200063006f007000690061007300200064006500200070007200650069006d0070007200650073006900f3006e0020006400650020006d00610079006f0072002000630061006c0069006400610064002e0020004c006f007300200064006f00630075006d0065006e0074006f00730020005000440046002000730065002000700075006500640065006e00200061006200720069007200200063006f006e0020004100630072006f00620061007400200079002000520065006100640065007200200035002e003000200079002000760065007200730069006f006e0065007300200070006f00730074006500720069006f007200650073002e0020004500730074006100200063006f006e0066006900670075007200610063006900f3006e0020007200650071007500690065007200650020006c006100200069006e0063007200750073007400610063006900f3006e0020006400650020006600750065006e007400650073002e> /SUO <FEFF004e00e4006900640065006e002000610073006500740075007300740065006e0020006100760075006c006c006100200076006f0069006400610061006e0020006c0075006f006400610020005000440046002d0061007300690061006b00690072006a006f006a0061002c0020006a006f006900640065006e002000740075006c006f0073007400750073006c00610061007400750020006f006e0020006b006f0072006b006500610020006a00610020006b007500760061006e0020007400610072006b006b007500750073002000730075007500720069002e0020005000440046002d0061007300690061006b00690072006a0061007400200076006f0069006400610061006e0020006100760061007400610020004100630072006f006200610074002d0020006a0061002000520065006100640065007200200035002e00300020002d006f0068006a0065006c006d0061006c006c0061002000740061006900200075007500640065006d006d0061006c006c0061002000760065007200730069006f006c006c0061002e0020004e00e4006d00e4002000610073006500740075006b0073006500740020006500640065006c006c00790074007400e4007600e4007400200066006f006e0074007400690065006e002000750070006f00740075007300740061002e> /ITA <FEFF00550073006100720065002000710075006500730074006500200069006d0070006f007300740061007a0069006f006e00690020007000650072002000630072006500610072006500200064006f00630075006d0065006e00740069002000500044004600200063006f006e00200075006e00610020007200690073006f006c0075007a0069006f006e00650020006d0061006700670069006f00720065002000700065007200200075006e00610020007100750061006c0069007400e00020006400690020007000720065007300740061006d007000610020006d00690067006c0069006f00720065002e0020004900200064006f00630075006d0065006e00740069002000500044004600200070006f00730073006f006e006f0020006500730073006500720065002000610070006500720074006900200063006f006e0020004100630072006f00620061007400200065002000520065006100640065007200200035002e003000200065002000760065007200730069006f006e006900200073007500630063006500730073006900760065002e002000510075006500730074006500200069006d0070006f007300740061007a0069006f006e006900200072006900630068006900650064006f006e006f0020006c002700750073006f00200064006900200066006f006e007400200069006e0063006f00720070006f0072006100740069002e> /NOR <FEFF004200720075006b00200064006900730073006500200069006e006e007300740069006c006c0069006e00670065006e0065002000740069006c002000e50020006f00700070007200650074007400650020005000440046002d0064006f006b0075006d0065006e0074006500720020006d006500640020006800f80079006500720065002000620069006c00640065006f00700070006c00f80073006e0069006e006700200066006f00720020006800f800790020007500740073006b00720069006600740073006b00760061006c00690074006500740020006600f800720020007400720079006b006b002e0020005000440046002d0064006f006b0075006d0065006e0074006500720020006b0061006e002000e50070006e006500730020006d006500640020004100630072006f0062006100740020006f0067002000520065006100640065007200200035002e00300020006f0067002000730065006e006500720065002e00200044006900730073006500200069006e006e007300740069006c006c0069006e00670065006e00650020006b0072006500760065007200200073006b00720069006600740069006e006e00620079006700670069006e0067002e> /SVE <FEFF0041006e007600e4006e00640020006400650020006800e4007200200069006e0073007400e4006c006c006e0069006e006700610072006e00610020006e00e40072002000640075002000760069006c006c00200073006b0061007000610020005000440046002d0064006f006b0075006d0065006e00740020006d006500640020006800f6006700720065002000620069006c0064007500700070006c00f60073006e0069006e00670020006600f60072002000700072006500700072006500730073007500740073006b0072006900660074006500720020006100760020006800f600670020006b00760061006c0069007400650074002e0020005000440046002d0064006f006b0075006d0065006e00740065006e0020006b0061006e002000f600700070006e006100730020006d006500640020004100630072006f0062006100740020006f00630068002000520065006100640065007200200035002e003000200065006c006c00650072002000730065006e006100720065002e00200044006500730073006100200069006e0073007400e4006c006c006e0069006e0067006100720020006b007200e400760065007200200069006e006b006c00750064006500720069006e00670020006100760020007400650063006b0065006e0073006e006900740074002e> >> >> setdistillerparams << /HWResolution [2400 2400] /PageSize [612.000 792.000] >> setpagedevice