MAT1252D Lecture 1 - Bases Number Representation
MAT1252D Lecture 1 - Bases Number Representation
1
Outline
3
Introduction
5
Introduction
6
The decimal number system
We are familiar with the decimal number system, which is based on the
number ten. The decimal system uses positional notation.
For example
"354" represents (3 100) + (5 10) + (4 1)
= (3 102) + (5 10) + (4 1)
whereas
“2435" represents (2 x 103) + (4 102) + (3 10) + (5 1).
For example
9
The octal number system
35 3x8 + 5x1 29
54 5x8 + 4x1 44
77 7x8 + 7x1 63
261 2x82 + 6x8 +1x1 177
10
Counting in octal
64 8 1 Dec. Equiv
0 0
1 1
2 2
... ...
7 7
1 0 8
1 1 9
... ... ...
1 7 15
2 0 16
2 1 17
... ... ...
7 7 63
1 0 0 64
1 0 1 65
... ... ... ...
1 7 7 127
2 0 0 128 11
Conversion of octal to decimal
12
Conversion of octal to decimal
13
Conversion of octal to decimal
14
Conversion from decimal to octal
The algorithm is: "Divide by 8 until the answer is 0, then read the
remainders in reverse order"
Example: Convert 17110 to octal.
When we divide 171 repeatedly by 8, we get:
15
Conversion from decimal to octal
8 278
8 34 + rem 6
8 4 + rem 2
8 0 + rem 4
So 27810 = 4268
111 4+2+1 7
1001 8+0+0+1 9
110011 32+16+0+0+2+1 51
10000000 128+0+...+0 128 17
16 8 4 2 1 Dec. equiv
Counting in binary 0
0
0
0
0
0
0
0
0
1
0
1
0 0 0 1 0 2
0 0 0 1 1 3
0 0 1 0 0 4
0 0 1 0 1 5
0 0 1 1 0 6
0 0 1 1 1 7
0 1 0 0 0 8
0 1 0 0 1 9
0 1 0 1 0 10
0 1 0 1 1 11
0 1 1 0 0 12
0 1 1 0 1 13
0 1 1 1 0 14
0 1 1 1 1 15
1 0 0 0 0 16
1 0 0 0 1 17
1 0 0 1 0 18
1 0 0 1 1 19
1 0 1 0 0 20
1 0 1 0 1 21
1 0 1 1 0 22
1 0 1 1 1 23
1 1 0 0 0 24
1 1 0 0 1 25
1 1 0 1 0 26
1 1 0 1 1 27
1 1 1 0 0 28
1 1 1 0 1 29
1 1 1 1 0 30
1 1 1 1 1 31
18
Conversion from binary to decimal
19
Conversion from binary to decimal
20
Conversion from binary to decimal
Method 2
1. Convert to octal by separating groups of three binary digits,
starting at the units digit. Then replace each group of three binary digits
by its octal equivalent.
0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111
22
Conversion from binary to decimal
23
Conversion from decimal to binary
2 58
2 29 + rem 0
2 14 + rem 1
2 7 + rem 0
2 3 + rem 1
2 1 + rem 1
0 + rem 1
8 370
8 46 + rem 2
8 5 + rem 6
8 0 + rem 5
Now replace each octal digit with the corresponding binary equivalent.
So 37010 = 5628 = 1011100102
27
The hexadecimal number system
This system is based on the number 16. Its main purpose is to help us
to cope with long strings of binary digits. For example, suppose we
had to remember the binary number:
00101101111100111000110001001011
We would find it much easier to work with its hexadecimal equivalent
2DF38C4B
1 5 21
1 0 3 259
5 7 87
1 0 0 0 4096
4 6 70
28
The hexadecimal number system
30
Counting in hexadecimal
163 162 16 1 Dec. equiv.
0 0 0 0 0
0 0 0 1 1
0 0 0 2 2
... ... ... ... ...
0 0 0 9 9
0 0 0 A 10
... ... ... ... ...
0 0 0 F 15
0 0 1 0 16
0 0 1 1 17
... ... ... ... ...
0 0 1 D 29
0 0 1 E 30
0 0 1 F 31
0 0 2 0 32
... ... ... ... ...
0 0 9 F 159
0 0 A 0 160
... ... ... ... ...
0 0 F F 255
0 1 0 0 256
... ... ... ... ...
F F F F 65535 31
Conversion of hexadecimal numbers to binary
0 0000 8 1000
1 0001 9 1001
2 0010 A 1010
3 0011 B 1011
4 0100 C 1100
5 0101 D 1101
6 0110 E 1110
7 0111 F 1111
Example 1:
Convert 1011111010101100112 to hexadecimal.
Example 2:
Convert 11111000010111011012 to hexadecimal.
33
Conversion between hexadecimal and decimal
Examples:
34
Conversion between hexadecimal and decimal
35
Conversion between hexadecimal and decimal
36
Representation of binary fractions
Here are some decimal numbers with both integer parts and fractional
parts: 1 1 1
... 100 10 1 ----- ----- ----- ... Meaning
10 100 1000
4 3 . 2 5 43.25
6 . 0 7 1 6.071
9 8 5 . 3 0 2 985.302
0 . 9 9 0 0.99
0 . 3 2 4 0.324
37
Representation of binary fractions
Notice that the fraction 0.324 has 3 places after the decimal point so the
1
last place represents 3 .
10
In the binary system, the columns to the right of the point represent
1 1 1 1 1 1 1 1
, , , … and so on. In other words, , 2 , 3 , 4 , …
2 4 8 16 2 2 2 2
38
Representation of binary fractions
Here are some binary numbers with both integer parts and fractional
parts:
1 1 1 1 Binary Decimal
... 4 2 1 ---- ---- ---- ---- ... meaning meaning
2 4 8 16
1 . 1 1.1 1.5
0 . 1 1 0.11 0.75
1 1 . 0 1 11.01 3.25
1 0 1 . 1 1 1 101.111 5.875
0 . 1 1 0 1 0.1101 0.8125
39
Representation of binary fractions
1 1 0 1 13
0.11012 means 2 + 4 + 8 + 16 or 16 = 0.8125 in decimal.
Here the fraction 0.1101 has 4 places after the decimal point so the last
𝟏
place represents 𝟒 .
𝟐
Example
1 1 1 19
0.100112 means + + or or 0.59375 in decimal
2 16 32 32
40
Converting binary fractions to decimal
41
Converting binary fractions to decimal
85
Therefore 0.0010101012 = = 0.16601562510
512
42
Converting binary fractions to decimal
5
Therefore 0.000001012 = = 0.0195312510
256
43
Converting decimal fractions to binary
Method
Repeatedly multiply the digits to the right of the decimal point by 2,
until only zeros remain. Then read the integer parts of the successive
answers, from top to bottom.
So 0.45312510 = 0.0111012
45
Converting decimal fractions to binary
0.65625
2
1.31250
2
0.62500
2
1.25000
2
0.50000
2
1.00000
So 0.6562510 = 0.101012
46
Summary of number conversion
Binary to
Octal to
Hexadecimal Substitution 12348 = 001 010 011 1002 = 0010 1001 11002 = 29C16
47
Summary of number conversion
Hexadecimal to
48
Summary of number conversion
Conversion Method Example
49
Arithmetic using other number systems
1 1
2 8 5
4 3 8 +
7 2 3
50
Addition in octal
Remember that in octal the digits represent 1, 8, 82, 83, and so on.
So, if the sum of the two digits in the 1's column is 8 or more, there will
be a carry of 1 into the next column, and so on.
Example 1 :
1 1
2 7 5
3 6 7 +
6 6 4
In 1st column, 78 + 58 (= 1210 = 8 + 4) = 148 , so the carry is 1.
In 2nd column, 68 + 78 + 18 = 168, so the carry is 1.
In 3rd column, 18 + 28 + 38 = 68.
51
Addition in octal
Example 2:
1 1 1
4 5 3 6
2 2 4 5 +
7 0 0 3
Example 1:
1 1 1 1
1 0 1 1
1 0 1 +
1 0 0 0 0
53
Addition in binary
Example 2:
1 1 1 1 1
1 1 1 0 1
1 0 1 1 1 +
1 1 0 1 0 0
Example 3:
1 1 1 1 1
1 0 0 1 1 0 1 0 1
1 0 1 1 1 1 0 +
1 1 0 0 1 0 0 1 1
55
Addition in binary
Example 3:
1 1 1 1 1
1 0 0 1 1 0 1 0 1
1 0 1 1 1 1 0 +
1 1 0 0 1 0 0 1 1
56
Addition in hexadecimal
Examples: 1 1
6 1 3 A 8 5
3 7 + C 5 + 9 D +
9 8 F F 1 2 2
1 1 1 1 1 1
B 9 A B C C
7 E + C D + 4 D +
1 3 7 1 7 8 1 1 9
57
Addition in hexadecimal
Examples: 1 1
6 1 3 A 8 5
3 7 + C 5 + 9 D +
9 8 F F 1 2 2
1 1 1 1 1 1
B 9 A B C C
7 E + C D + 4 D +
1 3 7 1 7 8 1 1 9
58
Binary Coded Decimal code (BCD)
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
59
Binary Coded Decimal code (BCD)
The remaining 4-bit binary numbers 1010, 1011, 1100, 1101, 1110 and
1111, which represent A, B C, D, E and F in hexadecimal, do not
correspond to any decimal digit, and are called "invalid BCD code
groups".
Example 1:
For the decimal number 37, its BCD representation is 0011 0111
3 7
Example 2:
For the decimal number 65, the BCD representation is 0110 0101
6 5
And the binary representation is 1000001.
Example 3:
For the decimal number 123, the BCD representation is
0001 0010 0011
1 2 3
61
Binary Coded Decimal code (BCD)
Example 4:
4 7 8
0100 0111 1000
62
Binary Coded Decimal code (BCD)
Example 5:
3 4 5 6 0 2
0011 0100 0101 0110 0000 0010
63
Binary Coded Decimal code (BCD)
Example 6:
Convert the BCD number 0101011100000001 into its decimal equivalent
0101 0111 0000 0001
5 7 0 1
Example 7:
The binary sequence 0100 1011 0011 is NOT a valid BCD representation,
because when we try to convert it into its decimal equivalent, we find
0100 1011 0011
4 ? 3
Binary (or hex) additions may or may not look like decimal additions.
65
Addition of BCD numbers
3 5 1 6 4 3
2 4 + 3 1 + 5 2 +
5 9 4 7 9 5
However the following three hex additions do not give correct decimal
answers:
2 4 7 5 3 8
3 6 + 8 7 + 3 9 +
5 A F C 7 1
66
Addition of BCD numbers
When two BCD code groups are added, there are three possible types
of outcomes as illustrated by the following three cases. Remember the
additions are all hex:
(i) (ii) (iii)
3 6 9
4 + 7 + 8 +
7 D 1 1
In case (i), if the result is interpreted as decimal, a correct answer is
obtained.
In case (ii) an invalid BCD code group is produced so the result cannot
be interpreted as decimal.
In case (iii), the result, if interpreted as decimal, gives a wrong answer.
67
Addition of BCD numbers
BCD addition means doing addition in hex but adjusting the answer so
that it makes sense when the inputs are interpreted in decimal.
Although all the calculations are in hex (or binary), as far as the user
can see, the operation is simply decimal addition.
The steps involved in BCD addition are shown in the following slide.
Notice that the first addition and then the decimal adjustment on each
column are completed before moving to the next column.
68
Addition of BCD numbers
Start at the right hand column (note: we work on one column at a time):
• Add the two digits and transfer any carry to the next column;
• Make the decimal adjustment by adding 6 or 0, according to the rule:
• If the sum of the two digits is more than 9, add 6
• Otherwise add 0.
• Add the two newly obtained digits of the column and transfer any
carry to the next column.
• Move to the next column and repeat the above operations, until all
columns have been processed.
Note that a carry may come from the first addition or from the second
69
addition.
Addition of BCD numbers
Example 1
1 1
Input by
user { 2
5
8
7
4
6 +
8 0 A The user does not
see these two rows
0 6 6 +
User interprets
output as decimal 8 6 0
70
Addition of BCD numbers
Example 2
1 1
Input by
user { 3
2
4
8
8
9 +
5 D 1 The user does not
see these two rows
0 6 6 +
User interprets
output as decimal 6 3 7
71
Addition of BCD numbers
1 1 1
4 5 8 4 7 5 3 6 8
5 2 6 + 5 0 3 + 5 3 7 +
9 8 E 9 7 8 9 A F
0 0 6 + 0 0 0 + 0 6 6 +
9 8 4 9 7 8 9 0 5
1 1 1 1 1 1 1
7 4 7 2 5 3 9 8 2
5 6 9 + 7 7 6 + 6 4 7 +
1 D B 0 1 A C 9 1 0 C 9
0 6 6 6 + 0 6 6 0 + 0 6 6 0 +
1 3 1 6 1 0 2 9 1 6 2 9 72
Addition of BCD numbers
More examples!
1 1 1 1 1 1 1 1 1 1
7 8 7 3 3 1 0 9 2 5 7 9 6
8 5 8 7 + 7 0 8 7 + 7 6 0 7 +
1 0 E 0 A 1 A 1 9 0 3 D E A D
0 6 6 6 6 + 0 6 0 0 6 + 0 6 6 6 6 +
1 6 4 6 0 1 0 1 9 6 3 3 4 0 3
73
The End
74