3. 3
Binary Coded Decimal - BCD
In computing and electronic systems, binary-coded decimal (BCD) is a class of
binary encodings of decimal numbers where each digit is represented by a fixed
number of bits
BCD was commonly used for displaying alpha-numeric in the past but in
modern-day BCD is still used with real-time clocks or RTC chips to keep track of
wall-clock time and it's becoming more common for embedded microprocessors to
include an RTC.
It's very common for RTCs to store the time in BCD format
4. 4
BCD Vs Binary
Both binary-coded decimal (BCD) and binary numbers are used in
many digital applications.
Both have their advantages and disadvantages.
BCD is commonly used when decimal numbers must be represented in
hardware, as each 4-bit BCD number maps directly to a decimal
number.
Binary is more efficient for arithmetic, memory storage, and
transmitting information, but is less human-readable.
5. 5
Binary Coded Decimal - BCD
Each single decimal digit represented in binary Coded to 4-bit binary
number
Radix/Base for decimal number system = 10 So, 10 values possible in
each digit: 0 to 9
In binary, Radix/Base is = 2 So, only 2 values possible in each digit: 0
and 1
6. 6
For example,
143 is a decimal number 1, 4 and 3 are the separate digits
Each of the digits 1, 4 and 3, needs to be converted (coded) to binary
separately
Each digit will be represented as 4 digit binary number
7. 7
BCD uses ‘positional weights’
Each decimal digit, in the 4-bit representation, is expressed
in terms of the positional weights
The ‘positional weights’ used in BCD are:
8-4-2-1
8. 8
Each bit – binary digit can contain either
0 or 1
2 values possible in each bit –
expressed in power of 2
8 – 4 – 2 – 1 is better
understood as
23
– 22
– 21
– 20
Here, 20 represents the LSB and 23 represents the MSB
for the 4-bit binary code
9. 9
Note
We use 4 bits in BCD, so 24
= 16 values can be represented
Out of 16, we use only 10 decimal digits, 0 to 9
Rest of the values 10 to 15 are invalid,
as they are decimal numbers, not digits
Note: BCD is the same as 4-bit Binary representation for decimal
digits 0 to 9
10. 10
Representing Decimal Digit 0
Decimal Digit Binary Coded Decimal
8 4 2 1 Final
0 0 0 0 0 0 0 0 0
Note: the Decimal Digit is expressed in terms of positional weights,
place 1 in case of the positional weights that are required,
for all others place 0
11. 11
Representing Decimal Digit 1
Decimal Digit Binary Coded Decimal
8 4 2 1 Final
1 0 0 0 1 0 0 0 1
Note: the Decimal Digit is expressed in terms of positional weights,
place 1 in case of the positional weights that are required,
for all others place 0
12. 12
Representing Decimal Digit 2
Decimal Digit Binary Coded Decimal
8 4 2 1 Final
2 0 0 1 0 0 0 1 0
Note: the Decimal Digit is expressed in terms of positional weights,
place 1 in case of the positional weights that are required,
for all others place 0
13. 13
Representing Decimal Digit 3
Decimal Digit Binary Coded Decimal
8 4 2 1 Final
3 0 0 1 1 0 0 1 1
Note: the Decimal Digit is expressed in terms of positional weights,
place 1 in case of the positional weights that are required,
for all others place 0
14. 14
Representing Decimal Digit 4
Decimal Digit Binary Coded Decimal
8 4 2 1 Final
4 0 1 0 0 0 1 0 0
Note: the Decimal Digit is expressed in terms of positional weights,
place 1 in case of the positional weights that are required,
for all others place 0
15. 15
Representing Decimal Digit 5
Decimal Digit Binary Coded Decimal
8 4 2 1 Final
5 0 1 0 1 0 1 0 1
Note: the Decimal Digit is expressed in terms of positional weights,
place 1 in case of the positional weights that are required,
for all others place 0
16. 16
Representing Decimal Digit 6
Decimal Digit Binary Coded Decimal
8 4 2 1 Final
6 0 1 1 0 0 1 1 0
Note: the Decimal Digit is expressed in terms of positional weights,
place 1 in case of the positional weights that are required,
for all others place 0
17. 17
Representing Decimal Digit 7
Decimal Digit Binary Coded Decimal
8 4 2 1 Final
7 0 1 1 1 0 1 1 1
Note: the Decimal Digit is expressed in terms of positional weights,
place 1 in case of the positional weights that are required,
for all others place 0
18. 18
Representing Decimal Digit 8
Decimal Digit Binary Coded Decimal
8 4 2 1 Final
8 1 0 0 0 1 0 0 0
Note: the Decimal Digit is expressed in terms of positional weights,
place 1 in case of the positional weights that are required,
for all others place 0
19. 19
Representing Decimal Digit 9
Decimal Digit Binary Coded Decimal
8 4 2 1 Final
9 1 0 0 1 1 0 0 1
Note: the Decimal Digit is expressed in terms of positional weights,
place 1 in case of the positional weights that are required,
for all others place 0
21. 21
2 types of Conversions
Decimal Number
BCD
BCD
Decimal Number
22. 22
Decimal Number to BCD Example 1
Q) Find BCD for Decimal Number 13
●(13)10 is a decimal number, not digit
●2 decimal digits in (13)10, 1 and 3
●First, we need to convert each digit to corresponding BCD value
●Then we place the BCD values together in proper order to get the
final BCD representation
24. 24
Final BCD of Decimal Number 13
●Therefore, BCD of (13)10 is:
●0001 0011
BCD of 1 BCD of 3
25. 25
Decimal Number to BCD Example 2
●(174)10 is a decimal number, not digit
●3 decimal digits in (174)10 - 1, 7 and 4
●First, we need to convert each digit to
corresponding BCD value
●Then we place the BCD values together in
proper order to get the final BCD
representation
27. 27
Final BCD of Decimal Number 174
●Therefore, BCD of (174)10 is:
●0001 0111 0100
BCD of 1 BCD of 7 BCD of 4
28. 28
BCD to Decimal Number
Conversion
●The BCD code for each decimal digit is of 4-
bit length
●So for any given BCD code,
●Starting from the right,
find the 4-bit groups in the given BCD
●Using the conversion table, determine the
decimal equivalent for each BCD code
●Place all decimal digits together to find
the final decimal number
29. 29
BCD to Decimal Number Example
1
q. Find Decimal Number for BCD 00010101
●Starting with the LSB, make group of 4-bits
●0001 0101
●
●So, we find 2 groups, means 2 decimal digits
●Look up the corresponding decimal digit for
each BCD code in the conversion table
30. 30
Finding the corresponding
decimal digits
Decimal Digit Binary Coded Decimal
8 4 2 1 Final
1 0 0 0 1 0 0 0 1
Decimal Digit Binary Coded Decimal
8 4 2 1 Final
5 0 1 0 1 0 1 0 1
So, decimal digit for BCD 0001 is 1
So, decimal digit for BCD 0101 is 5
32. 32
BCD to Decimal Number Example 2
●In many cases, the BCD code may not be
easily seperated in groups of 4-bits
●
q. Find Decimal Number for BCD 100001
●Here, the given BCD is of length 6 !
33. 33
Solution
●In this case, the grouping will be:
10 0001
●Clearly, the first group is not a valid BCD code
●To make it valid, add required number of 0s at the
left of the number to change it to 4-bits
●So, the groups now become:
0010 0001
●2 zeroes (highlighted in red) are added before the
MSB to make make 4-bit BCD
34. 34
Finding the corresponding
decimal digits
Decimal Digit Binary Coded Decimal
8 4 2 1 Final
2 0 0 1 0 0 0 1 0
Decimal Digit Binary Coded Decimal
8 4 2 1 Final
1 0 0 0 1 0 0 0 1
So, decimal digit for BCD 0001 is 2
So, decimal digit for BCD 0001 is 1
38. 38
ASCII Code
American Standard Code for Information
Interchange – ASCII
127 different characters represented
127 = 27 – 1, so ASCII is a 7-bit code
Codes are numbered from 0 to 126
40. 40
Some important ASCII codes
Characters ASCII Code
0 to 9 48 to 57
(A to Z) 65 to 90
(a to z) 97 to 122
For alphabets,
Think of index starting wit
A 0 65
B 1 65+1
C 2 65+2
D 3 65+3
.........
J 9 65+9=74
a 0 97
b 1 98=97+1
c 2 99=97+2
d 3 100
...............
j 9 97+9=106
41. 41
Binary to ASCII conversion
●Starting from the right (LSB),
Group every 7 bits together
●For each group of 7 bits,
Determine the corresponding decimal
value
●Use ASCII table to find the ASCII code
for each group
42. 42
Binary to ASCII conversion
Example 1
q. Convert 10010110110001 to ASCII
●First, we make 7-bit groups, starting with
the LSB
●1001011 0110001
●Next, we convert each 7-bit binary to
decimal number
44. 44
●Finally, we find the corresponding
ASCII values
75 in ASCII = K
49 in ASCII = 1
●So, 10010110110001 in ASCII is K1
45. 45
ASCII to Binary Conversion
Example 1
q. Convert Mp4 in ASCII to Binary
●First, find the corresponding decimal
numbers for each ASCII character
●M p 4
77 112 52
47. 47
●Next, each of the binary numbers will placed in order
of their corresponding decimal numbers
●So,
77 112
52
1001101 1110000
0110100
●Since we are converting ASCII to binary, all binary
representations have to be of 7-bits
●Here, binary for 52 is of 6 bits, so we add 0 at LSB
48. 48
●So, each binary has to be converted to
7-bit form by adding 0s to the LSB as
required
●Finally, the Binary form of ASCII Mp4
is =
●
(1001101 1110000 0110100)2
50. 50
EBCDIC Code
●Extended Binary Coded Decimal Interchange
Code
●8-bit representation in binary form
●Mainly used in IBM Mainframe machines
●Extension of Binary Coded Decimal form
●Makes use of fixed, 4-bit ‘Zone’ bits
with
4-bit binary representation of digits etc.
51. 51
EBCDIC Code equivalent for
Digits 0-9
Character EBCDIC Code
Zone Digit
0 1111 0000
1 1111 0001
2 1111 0010
3 1111 0011
4 1111 0100
5 1111 0101
6 1111 0110
7 1111 0111
8 1111 1000
9 1111 1001
●4-bit Zone bits for digits is 1111
●The 4-bit binary representation is same as BCD codes
52. 52
EBCDIC Code equivalent for
Alphabets
Character EBCDIC
Zone Digits
A 1100 0001
B 1100 0010
C 1100 0011
D 1100 0100
E 1100 0101
F 1100 0110
G 1100 0111
H 1100 1000
I 1100 1001
●The 26 alphabets are divided in 3 groups
A to I (9),
J to R (9), and
S to Z (8)
●Each group has separate 4-bit Zone bits:
A to I: 1100
●For each group, the digit value goes from 1 to 9, and
●corresponding 4-bit binary is used
53. 53
EBCDIC Code equivalent for
Alphabets
Character EBCDIC
Zone Digit
J 1101 0001
K 1101 0010
L 1101 0011
M 1101 0100
N 1101 0101
O 1101 0110
P 1101 0111
Q 1101 1000
R 1101 1001
●For this group, the 4-bit Zone bits are
J to R: 1101
●The digit value goes from 1 to 9, and so the same corr
54. 54
EBCDIC Code equivalent for
Alphabets
Character EBCDIC
Zone Digit
S 1110 0010
T 1110 0011
U 1110 0100
V 1110 0101
W 1110 0110
X 1110 0111
Y 1110 1000
Z 1110 1001
●For this group, the 4-bit Zone bits are
S to Z: 1110
●The digit value goes from 2 to 9, and so the same
● corresponding 4-bit binary is used
55. 55
Decimal Number to EBCDIC Example
1
q. Convert 425 to EBCDIC
●First, determine the zone bits – here it is
1111 (for digits)
●Next, find the BCD of each decimal digit:
●4 5 2
0100 0010 0101
56. 56
●To the BCDs, append the Zone bits at LSB
4 2
5
11110100 11110010
11110101
●Bring the 8-bit codes together in order to get
the final EBCDIC representation:
●11110100 11110010 11110101
57. 57
EBCDIC to Character Example 1
q. Convert the following EBCDIC to
Decimal:
1111100111111000
●First, make groups of 8-bits each
starting from the right
11111001 11111000
58. 58
●The first 4-bits of each group
represents Zone bits,use it to identify
the type of data
●Here Zone bits = 1111, so we have digits
●For each group, starting with the right,
extract the 4 digit bits and find their
corresponding decimal value
●11111001 11111000
1001 1000
59. 59
In decimal,
1001
3 2 1 0
= 1x20 +1x23 = 9
1000
3 2 1 0
= 1x23 = 8
So, placing the decimal digits in order, the
converted value of given EBCDIC code = 98
61. 61
Gray Code
●Named after scientist Frank Gray
●Called ‘Reflected Binary code’
●Expressions for sequential numbers differ
only by 1 bit
●So, binary expressions are converted to
Gray Code to minimize switching operations
●Also called ‘Unit Distance Code’, ‘Cyclic
Code’
62. 62
Binary to Gray Code conversion
●Take the MSB – this is the MSB for Gray
Code
●Add MSB to next bit of Binary, keep the
sum and ignore carry (XOR) – the sum is
the 2nd bit after MSB in Gray Code
●Keep shifting bit by bit towards right,
and repeat the above two steps to obtain
the complete Gray Code
63. 63
Generalized Rule
●If a binary number b3b2b1b0 is to be
converted to Gray Code g3g2g1g0,
●g3 = b3,
●g2=b3⊕b2
●g1=b2⊕b1
●g0=b1⊕b0
64. 64
Example of Binary to Gray Code
convertion
q. Convert 1101 to Gray Code
Step 1:
●MSB of 1101 is 1
●So, MSB of Gray
Code is set as 1
Binary: 1 1 0 1
Gray Code:1
65. 65
Example of Binary to Gray Code
convertion
Step 2:
●Add MSB (1) to ne
●Set the sum (0) a
●Ignore the carry
Binary: 1 1 0 1
+
Gray Code: 1 0
66. 66
Example of Binary to Gray Code
convertion
Step 3:
●Add succeeding bi
●Set the sum (1) a
●Ignore the carry
Binary: 1 1 0 1
+
Gray Code: 1 0 1
67. 67
Example of Binary to Gray Code
conversion
Step 4:
●Add succeeding bi
●Set the sum (1) a
●Ignore the carry
Binary: 1 1 0 1
+
Gray Code: 1 0 1 1
68. 68
Gray Code to Binary conversion
●Take the MSB – this is the MSB for Gray
Code
●Add MSB to next bit of Gray Code, keep
the sum and ignore carry (XOR) – the sum
is the 2nd bit after MSB
●Keep shifting bit by bit towards right,
and repeat the above two steps to obtain
the complete Gray Code
69. 69
Generalized Rule
●If a Gray Code g3g2g1g0 is to be
converted to binary number b3b2b1b0,
●b3 = g3,
●b2=b3⊕g2
●b1=b2⊕g1
●b0=b1⊕g0
70. 70
Example of Gray Code to Binary
conversion
q. Convert 1011 to Binary
Step 1:
●MSB of 1011 is 1
●So, MSB of Binary
Gray Code: 1 0 1 1
Binary: 1
71. 71
Example of Gray Code to Binary
conversion
Step 2:
●XOR the MSB (1) t
●Set the result (1
Gray Code: 1 0 1 1
⊕
Binary: 1 1
72. 72
Example of Binary to Gray Code
convertion
Step 3:
●XOR the succeesin
●Set the result (0
Gray Code: 1 0 1 1
⊕
Binary: 1 1 0
73. 73
Example of Binary to Gray Code
conversion
Step 4:
●XOR the succeesin
●Set the result (1
Gray Code: 1 0 1 1
⊕
Binary: 1 1 0 1