0% found this document useful (0 votes)
38 views74 pages

MAT1252D Lecture 1 - Bases Number Representation

The document outlines a lecture on number systems, focusing on decimal, octal, binary, and hexadecimal systems, including their conversions and arithmetic operations. It aims to equip students with skills to count, convert integers and fractions, and perform addition in these systems. The lecture emphasizes the importance of understanding different bases for effective computation in computer science.

Uploaded by

aabidhali284
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views74 pages

MAT1252D Lecture 1 - Bases Number Representation

The document outlines a lecture on number systems, focusing on decimal, octal, binary, and hexadecimal systems, including their conversions and arithmetic operations. It aims to equip students with skills to count, convert integers and fractions, and perform addition in these systems. The lecture emphasizes the importance of understanding different bases for effective computation in computer science.

Uploaded by

aabidhali284
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 74

Lecture 01

Bases and Number


Representation

1
Outline

01. The decimal number system 14. Conversion of binary numbers to


hexadecimal
02. Use of subscripts
15. Conversion between hexadecimal and
03. The octal number system
decimal
04. Counting in octal
16. Representation of binary fractions
05. Conversion of octal to decimal
17. Converting binary fractions to decimal
06. Conversion from decimal to octal
18. Converting decimal fractions to binary
07. The binary number system
19. Summary of number conversion
08. Counting in binary
09. Conversion from binary to decimal 20. Arithmetic using other number systems
10. Conversion from decimal to binary 21. Addition in octal
11. The hexadecimal number system 22. Addition in binary
12. Counting in hexadecimal 23. Addition in hexadecimal
13. Conversion of hexadecimal 24. Binary Coded Decimal code (BCD)
numbers to binary 25. Addition of BCD numbers
2
Lecture’s Major Objectives

After completing this section, students should be able to:


• count in each of the decimal, octal, binary and hexadecimal
number systems
• convert integers between the number systems
• convert fractions between binary and decimal number systems
• perform addition in each of the number systems
• find the BCD code for a decimal number
• convert a BCD number to its decimal equivalent
• perform BCD additions, making any necessary decimal
adjustments

3
Introduction

Humans count on 10 fingers*…

*in many, but not all, cultures


4
Introduction

Computers count on switches!

5
Introduction

Computers count on switches!

This means we need to develop


a new type of counting based
on 2s instead of 10s.

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).

In the decimal system we use ten different symbols to represent


numbers.
These symbols are: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9
7
Different number systems

There is nothing special about the decimal number system for


representing numbers! Civilisations around the world and throughout
history have chosen different bases to count in.
The most common historical bases are those involving 5, 10 and 20.
E.g. the Mayans of South America used base 20 to count. Each column
represents successive powers of 20, and they had symbols 0-19 to use
in each column.

The most common bases for computers are


2, 8 and 16.
E.g. base 2 is called binary. Each column represents
a power of 2, and only the symbols 0 and 1 are needed.
8
Use of subscripts

In MAT1252, we use different subscripts to indicate which number


system we are using, and hence avoid ambiguity.

For example

35410 represents a decimal number (base 10)


3548 represents an octal number (base 8)
10010102 represents a binary number (base 2)
35416 represents a hexadecimal number (base 16)

9
The octal number system

The octal system is a positional system using base 8.


Octal uses only the 8 symbols 0, 1, 2, 3, 4, 5, 6 and 7.

(354)8 means (3  82) + (5  8) + (4  1) which is equal to 23610 and


(435)8 means (4  82) + (3  8) + (5  1) which is equal to 28510.

Octal number Meaning Dec. equivalent

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

To convert from octal to decimal, we recall that each octal digit


represents a particular power of 8.

Example 1: Convert 2138 to decimal.


2138 = (2  82) + (1  8) + (3  1)
= (2  64) + (1  8) + (3  1) = 13910

Example 2: Convert 4678 to decimal.


4678 = (4  82) + (6  8) + (7  1)
= (4  64) + (6  8) + (7  1) = 31110

12
Conversion of octal to decimal

Example 3: Convert 42768 to decimal.

13
Conversion of octal to decimal

Example 3: Convert 42768 to decimal.

42768 = (4  83) + (2  82) + (7  8) + (6  1)


= (4  512) + (2  64) + (7  8) + (6  1)
= 223810

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:

The remainders, in reverse order, are: 2, 5 and 3.


8 171 So 17110 = 2538
8 21 + rem 3
Why does this method work?
8 2 + rem 5 171 = (21  8) + 3 and
8 0 + rem 2 21 = (2  8) + 5, so
171 = [(2  8) + 5]  8 + 3
therefore 171 = (2  82) + (5  8) + 3

15
Conversion from decimal to octal

Example: Convert 27810 to octal.

8 278
8 34 + rem 6
8 4 + rem 2
8 0 + rem 4

So 27810 = 4268

[ Check: 4268 = (4  64) + (2  8) + (6  1) = 27810 ]


16
The binary number system

The binary system is a positional system using base 2.


In this system there are only two symbols, 0 and 1.

So, for example, the binary number 101 represents


(1  22) + (0  2) + (1  1) = 5 in decimal
and the binary number 1101 represents
(1  23) + (1  22) + (0  2) + (1  1) = 13 in decimal

Here are some binary numbers and their decimal equivalents:


Binary number Meaning Dec. equivalent

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

Method 1: Write each number as the sum of powers of 2

Example 1: Convert 101110012 to decimal.


101110012 = 27 + 25 + 24 + 23 + 1
= 128 + 32 + 16 + 8 + 1 = 18510

Example 2: Convert 1011110112 to decimal.


1011110112 = 28 + 26 + 25 + 24 + 23 + 2 + 1
= 256 + 64 + 32 + 16 + 8 + 2 + 1 = 37910

Example 3: Convert 1111002 to decimal.


1111002 = 25 + 24 + 23 + 22
= 32 + 16 + 8 + 4 = 6010

19
Conversion from binary to decimal

Method 1: Write each number as the sum of powers of 2

Example 1: Convert 101110012 to decimal.


101110012 = 27 + 25 + 24 + 23 + 1
= 128 + 32 + 16 + 8 + 1 = 18510

Example 2: Convert 1011110112 to decimal.


1011110112 = 28 + 26 + 25 + 24 + 23 + 2 + 1
= 256 + 64 + 32 + 16 + 8 + 2 + 1 = 37910

Example 3: Convert 1111002 to decimal.


1111002 = 25 + 24 + 23 + 22
= 32 + 16 + 8 + 4 = 6010

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

2. Convert from octal to decimal.


21
Conversion from binary to decimal

Example 1 (again): Convert 101110012 to decimal.


10 | 111 | 001 = 2718 = (2  64) + (7  8) + 1 = 18510
2 7 1

Example 2 (again): Convert 1011110112 to decimal.


101 111 011 = 5738 = (5  64) + (7  8) + 3 = 37910

Example 3 (again): Convert 1111002 to decimal.


111 100 = 748 = (7  8) + 4 = 6010

22
Conversion from binary to decimal

Example 1 (again): Convert 101110012 to decimal.


10 | 111 | 001 = 2718 = (2  64) + (7  8) + 1 = 18510
2 7 1

Example 2 (again): Convert 1011110112 to decimal.


101 111 011 = 5738 = (5  64) + (7  8) + 3 = 37910

Example 3 (again): Convert 1111002 to decimal.


111 100 = 748 = (7  8) + 4 = 6010

23
Conversion from decimal to binary

Method 1: Divide by 2 until the answer is 0, then read the remainders in


reverse order.
Example 1: Convert 5810 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

The remainders, in reverse order, are 1, 1, 1, 0, 1 and 0.


Therefore 5810 = 1110102
[ Check: 1110102 = 32 + 16 + 8 + 0 + 2 + 0 = 5810 ]
24
Conversion from decimal to binary

An alternative (and much quicker) method is to use octal.

First you need to again remember the binary equivalents of the


numbers 0, 1, 2, …, 7.
These are:
0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111
25
Conversion from decimal to binary

Method 2: First convert the decimal number to octal, and then


replace each octal digit by its binary equivalent.

Example 1 (again): Convert 5810 to binary


First convert to octal:
8 58
8 7 + rem 2
8 0 + rem 7

Reading the remainders in reverse order, we find 5810 = 728


Now replace each octal digit with the corresponding binary equivalent.
(In this case replace 7 by 111 and replace 2 by 010)
26
So 5810 = 728 = 1110102
Conversion from decimal to binary

Example 2: Convert 37010 to binary.

First convert to octal:

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

163 162 16 1 Dec. equiv.

1 5 21
1 0 3 259
5 7 87
1 0 0 0 4096
4 6 70
28
The hexadecimal number system

The hexadecimal system is a positional system using base 16.


Hexadecimal uses 16 symbols. The symbols 0, 1, 2, …, 9 have the
same meaning as in decimal, and we need six extra symbols to
represent these six numbers. We use A, B, C, D, E and F:

A represents the decimal number 10,


B represents the decimal number 11,
C represents the decimal number 12,
D represents the decimal number 13,
E represents the decimal number 14, and
F represents the decimal number 15.

"5C" in hex represents (5  16) + 12 = 92 in decimal.


"1AF" in hex represents (1  162) + (10  16) + (151) = 431 in decimal.
29
The hexadecimal number system

Here are some more hexadecimal numbers and their decimal


equivalents:
(Note: 162 = 256 and 163 = 4096)

Hex. number Meaning Dec. equiv.

2B7 2x256 + 11x16 + 7x1 695


496 4x256 + 9x16 + 6x1 1174
FE 15x16 + 14x1 254
CB4D 12x4096 + 11x256 + 4x16 +13x1 52045

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

First we need to be familiar with the binary equivalents of the


hexadecimal numbers 0, …, F. These are:

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

Then it is simply a matter of replacing each hexadecimal digit by the


corresponding four binary digits.
32
Conversion of binary numbers to hexadecimal

To convert a binary integer to hexadecimal, separate binary digits into


groups of four, starting at the units digit, and then replace each group
by its hexadecimal equivalent.

Example 1:
Convert 1011111010101100112 to hexadecimal.

10 1111 1010 1011 00112 = 2FAB316

Example 2:
Convert 11111000010111011012 to hexadecimal.

111 1100 0010 1110 11012 = 7C2ED16

33
Conversion between hexadecimal and decimal

Sometimes it is convenient to be able to convert from simple


hexadecimal numbers to decimal, or vice versa.

For conversions such as these it helps to know, or at least be familiar


with, the multiples of 16.

Examples:

1. The hex number 3C means (316) + 12 = 60 in decimal.

2. The hex number 54 means (516) + 4 = 84 in decimal.

3. The hex number F6 means (1516) + 6 = 246 in decimal.

34
Conversion between hexadecimal and decimal

4. To convert the decimal number 43 to hexadecimal:


43 divided by 16?
216 = 32, so
43 divided by 16 gives 2 with remainder 11
So 43 = (216) + 11
Therefore 43 in decimal becomes 2B in hex.

5. To convert the decimal number 77 to hexadecimal:


77 divided by 16?
416 = 64, so
77 divided by 16 gives 4 with remainder 13
So 77 = (416) + 13
Therefore 77 in decimal becomes 4D in hex.

35
Conversion between hexadecimal and decimal

6. To convert the decimal number 103 to hexadecimal:

103 divided by 16?


616 = 96, so
103 divided by 16 gives 6 with 7 remainder
So 103 = (616) + 7
Therefore 103 in decimal becomes 67 in hex.

36
Representation of binary fractions

Now we consider fractions in binary. The concepts are analogous to


those in the decimal system.
In the decimal system, the columns to the right of the decimal point
represent
1 1 1
, , , … and so on.
10 100 1000

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

In decimal, for example


1 1 1 324
0.324 means 3× + 2× 2 + 4× 3 =
10 10 10 1000

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

In the binary system, for example,

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

A simple method is:


1. Convert the fractional part (i.e. the number after the decimal place)
to a decimal number.
2. Divide your answer by the appropriate power of 2 as given by the
number of places in the fractional part.

Example 1: Convert 0.1100112 to decimal.

1. The number after the point is (110011)2 which is 51 in decimal.


2. The fraction has 6 places, so we must divide by 26 = 64.
51
Therefore 0.1100112 = = 0.79687510
64

41
Converting binary fractions to decimal

Example 2: Convert 0.111012 to decimal.

1. The number after the point is (11101)2 = 2910


2. The fraction has 5 places, so we must divide by 25 = 32.
29
Therefore 0.111012 = = 0.9062510
32

Example 3: Convert 0.0010101012 to decimal.

1. The number after the point is (1010101)2 = 8510 .


2. The fraction has 9 places, so we must divide by 29 = 512.

85
Therefore 0.0010101012 = = 0.16601562510
512
42
Converting binary fractions to decimal

Example 4: Convert 0.000001012 to decimal.

1. The number 101 in binary is 5 in decimal.


2. The fraction has 8 places, so we must divide by 28 = 256.

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.

Example 1: Convert the decimal fraction 0.34375 to binary.


0.34375
2
0.68750
2
1.37500
2
0.75000
2
1.50000
2
1.00000 So 0.3437510 = 0.010112 44
Converting decimal fractions to binary

Example 2: Convert the decimal fraction 0.453125 to binary.


0.453125
2
0.906250
2
1.812500
2
1.625000
2
1.250000
2
0.500000
2
1.000000

So 0.45312510 = 0.0111012

45
Converting decimal fractions to binary

Example 3: Convert the decimal fraction 0.65625 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

Conversion Method Example

Binary to

Octal Substitution 10111011001 2 = 10 111 011 001 2 = 27318

Hexadecimal Substitution 10111011001 2 = 101 1101 10012 = 5D916

Decimal Summation 10111011001 2 = 1x1024 + 0x512 + 1x256


+ 1x128 + 1x64 + 0x32 + 1x16 + 1x8 +
0x4 + 0x2 + 1x1 = 149710

Octal to

Binary Substitution 12348 = 001 010 011 1002

Hexadecimal Substitution 12348 = 001 010 011 1002 = 0010 1001 11002 = 29C16

Decimal Summation 12348 = 1x512 + 2x64 + 3x8 + 4x1 = 66810

47
Summary of number conversion

Conversion Method Example

Hexadecimal to

Binary Substitution C0DE16 = 1100 0000 1101 11102

Octal Substitution C0DE16 = 1100 0000 1101 11102


= 1 100 000 011 011 1102 = 1403368

Decimal Summation C0DE16 = 12x4096 + 0x256 + 13x16 + 14x1 = 4937410

48
Summary of number conversion
Conversion Method Example

“least significant bit”

“most significant bit”

49
Arithmetic using other number systems

We are mainly concerned with the addition of two numbers. An example


in the decimal system:

1 1
2 8 5
4 3 8 +
7 2 3

In 1st column, 8 + 5 = 13, so we "put down 3 and carry the 1".


In 2nd column, 3 + 8 + 1 = 12, we "put down 2 and carry the 1".
In 3rd column, 1 + 2 + 4 = 7.

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

In 1st column, 68 + 58 = 138 , so the carry is 1.


In 2nd column, 48 + 38 + 18 = 108 , so the carry is 1.
In 3rd column, 28 + 58 + 18 = 88 = 108, so the carry is 1.
In 4th column, 28 + 48 + 18 = 78.
52
Addition in binary

Example 1:

1 1 1 1
1 0 1 1
1 0 1 +
1 0 0 0 0

In 1st column, 1 + 1 = 10, so the carry is 1.


In 2nd column, 0 +1 + 1 = 10, so the carry is 1.
In 3rd column, 1 + 0 + 1 = 10 , so the carry is 1.
In 4th column, 1 + 1 = 10 , so the carry is 1.

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

In 1st column, 1 + 1 = 10, so the carry is 1.


In 2nd column, 1 + 0 + 1 = 10, so the carry is 1.
In 3rd column, 1 + 1 + 1 = 11 , so the carry is 1.
In 4th column, 0 + 1 + 1 = 10, so the carry is 1.
In 5th column, 1 + 1 + 1 = 11 , so the carry is 1.
54
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

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

In hexadecimal the digits represent 1, 16, 162, 163, and so on.


So, if the sum of the two digits in the 1's column is 16 or more, there
will be a carry of 1 into the next column, and so on.

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

In hexadecimal the digits represent 1, 16, 162, 163, and so on.


So, if the sum of the two digits in the 1's column is 16 or more, there
will be a carry of 1 into the next column, and so on.

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)

In BCD code, each digit of a decimal number is represented by a 4-bit


binary "code group". This is easier than decimal for computers.
Since there are 10 decimal digits, the 4-bit binary numbers for 0, 1, …, 9
are used for BCD representation, as shown in the table.
decimal BCD
digit code

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".

To convert from decimal to BCD we replace each decimal digit by its


4-bit binary code group.

Example 1:
For the decimal number 37, its BCD representation is 0011 0111
 
3 7

Note this is different to the binary representation, which is 100101.


60
Binary Coded Decimal code (BCD)

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

And the binary representation is 1111011.

61
Binary Coded Decimal code (BCD)

Example 4:

Fo the decimal number 478, the BCD representation is:

4 7 8
  
0100 0111 1000

62
Binary Coded Decimal code (BCD)

Example 5:

Consider the decimal number 345602. Its BCD representation is as


shown:

3 4 5 6 0 2
     
0011 0100 0101 0110 0000 0010

Now we want to convert from BCD code back to decimal.


This is done by
1. Separating the BCD number into groups of 4 bits
2. Replacing each group by the corresponding decimal digit.

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

We say 1011 is an invalid BCD code group.


64
Addition of BCD numbers

In the following discussion about addition of BCD numbers it is much


easier to use hexadecimal rather than binary representation.

So valid BCD code groups are represented by the hex numbers 0, 1, 2,


…, 9, while the hex numbers A, B, C, D, E and F represent invalid BCD
code groups.

When a microprocessor adds BCD numbers, it is really adding binary


(or hex) numbers, but the final output must look like the result of
decimal addition.

Binary (or hex) additions may or may not look like decimal additions.

65
Addition of BCD numbers

These three hex additions can be interpreted as correct decimal


additions:

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.

The hexadecimal addition of the inputs and the decimal adjustments


occur inside the machine and are therefore hidden from the user.

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

The procedure for BCD addition

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

Some further examples:

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

You might also like