Binary Coded Decimal (BCD)

This tutorial is based on Binary Coded Decimal (BCD). You will learn about code classifications, features of BCD code, interconversion of BCD and Decimal as well as BCD and Binary. You will also learn a few other weighted codes and see their comparisons with BCD.

Contents:

  1. Classifications of Codes
  2. What is BCD?
  3. Features of BCD
  4. BCD to Decimal Conversion
  5. Decimal to BCD Conversion
  6. Interconversion of BCD and Binary Numbers
  7. Other weighted codes
  8. Comparison between BCD and other weighted codes

Classification of Codes

Codes can be classified as weighted or unweighted codes, sequential or non-sequential codes, and complementary or non-complementary codes. Here are the definitions of each:

  1. Weighted and Unweighted code
    • Weighted Code: – If we express numbers as the sum of some other specific numbers, it is known as weighted code. These specific numbers are called positional weights. Examples of weighted code are BCD and 2-4-2-1 code.
    • Unweighted Code: – Unweighted codes do not use positional weights. Unweighted codes are usually a result of performing some logical operations on an existing weighted code. Some examples are Gray code and Excess-3 code.
  2. Sequential and Non-sequential code
    • Sequential Code: – In sequential codes, code for a number can be obtained by adding one to its previous number’s code. Examples of sequential codes are BCD and Excess-3 code.
    • Non-Sequential Code: – In non-sequential codes, code for a number cannot be obtained by adding one to its previous number’s code. An example of non-sequential code is the 2-4-2-1 code
  3. Self-Complementary and Non-self-complementary code
    • Self-Complementary: – In self-complementary code, 9’s complement of a decimal number can be obtained by 1’s complement of its coded form. Examples of self-complementary code are 2-4-2-1 and Excess-3 code.
    • Non-self-complementary: – In non-self-complementary code, there is no relation between a decimal number and its coded form. Numbers in this type of code cannot be obtained using complements. An example of this is the BCD code.

What is BCD?

BCD stands for binary coded decimal. It is another way of representing decimal numbers. It is a weighted code with positional values as 8-4-2-1. In BCD representation, we write decimal numbers as encodings of 4-binary bits. Here is a table that represents BCD.

DECIMAL NUMBER BCD ( 8 4 2 1 )
0 0 0 0 0
1 0 0 0 1
2 0 0 1 0
3 0 0 1 1
4 0 1 0 0
5 0 1 0 1
6 0 1 1 0
7 0 1 1 1
8 1 0 0 0
9 1 0 0 1

As shown in the table, any digit of the decimal system can be written as a sum of 8, 4, 2, 1. For example: –
7 can be written as 4 + 2 + 1, so its BCD representation is 0111.

advertisement
advertisement

Features of BCD Codes

Here are some of the features of BCD listed.

  • BCD is an efficient and faster way of representing numbers as we use the positional value system and for any number, we show it as an addition of those positional values.
  • In BCD, if we have to include a bit’s positional value, we encode it with 1.
  • BCD code uses the binary weights of 8-4-2-1 as positional values.
  • To represent a decimal number, either 4-bits or 8-bits can be used. Using 4-bits is known as packed BCD representation and 8-bits is known as unpacked BCD representation.
  • BCD numbers are decimal numbers only.
  • It is different from decimal to binary conversion and also different from hex-to-binary conversion as we only represent 0-9 digits and not till 16.

Conversion of Decimal to BCD

To convert a number from decimal to BCD, we write down each of the decimal digits as its equivalent BCD code separately. It is to be kept in mind, this is not equivalent to decimal to binary conversion.

For example,

  • (526)10 can be written in BCD as (0101 0010 0110)
    5 —-> 0101 2 —-> 0010 6 —–> 0110
  • (22)10 can be written in BCD as (0010 0010)
    2 ——> 0010
  • (784)10 can be written in BCD as (0111 1000 0100)
    7 —-> 0111 8—> 1000 4—-> 0100

Conversion of BCD to Decimal

To convert a BCD code into decimal, we make groups of four bits beginning from LSB and write down the equivalent decimal digits for each group individually. Some points to note are: –

  • This can only be done if the groups range between 0000-1001.
  • If even one group does not belong to this, the number is considered an invalid BCD number.
  • Also note, BCD to decimal conversion will give different results compared to binary to decimal conversion of the same number.

For example,

  • (01101001) BCD code in decimal is written as (69)10
    0110 —-> 6 1001 —-> 9
  • (00010101) BCD code in decimal is written as (15)10
    0001 —–> 1 0101 ——> 5
  • (11000101) is an invalid BCD number as 1100 has no decimal equivalent digit.

advertisement

Interconversion of BCD and Binary Numbers

Binary numbers can be converted into BCD by the following steps: –

  • First, convert the binary number into decimal using positional values.
  • Then convert the decimal number into its BCD equivalent.

For example, (11000101)2 in decimal is 1 x 27 + 1 x 26 + 1 x 22 + 1 x 20 = (197)10
197 in BCD is written as (0001 1001 0111)

BCD numbers can be converted into binary by the following steps: –

  • First, convert the BCD number into decimal by making groups of 4-bits and writing its equivalent.
  • Then convert the decimal number into binary using repeated division by 2 or by writing it as a sum of multiples of 2.

For example, (0101 0110) BCD code can be written in decimal as (56)10
56 in binary can be given as (32 + 16 + 8)
= (1 x 25 + 1 x 24 + 1 x 23)
= (111000)2

advertisement

Other Weighted Codes

Apart from BCD (8-4-2-1) code, we have other weighted codes like 2-4-2-1 {two-four-two-one} and 8-4-(-2)-(-1) {eight-four-minus two-minus one}. The following table shows the other two weighted codes.

Decimal Numbers 2-4-2-1 8 – 4 – (-2) – (-1)
0 0 0 0 0 0 0 0 0
1 0 0 0 1 0111
2 1 0 0 0 0110
3 0111 0101
4 0110 0100
5 0101 1011
6 1 1 0 0 1010
7 0 1 1 1 1001
8 1 1 1 0 1000
9 1 1 1 1 1111

As shown in the table,

  • Both the codes are self-complementary as the codes for 0 & 9, 1 & 8, 2 & 7, etc. are complementary to each other.
  • The positional values are selected such that the sum of positional values in each of these two codes result into 9. (2 + 2 + 4 + 1 = 9 and 8 + 4 – 2 – 1 = 9)
  • These codes are not sequential as the code for a digit cannot be obtained by adding one to its previous code.

Comparison between BCD and other weighted codes

Here is a table showing the comparison between BCD and the other two weighted codes.

Parameters BCD 2-4-2-1 8 – 4 – (-2) -(-1)
Weighted It is a weighted code. It is a weighted code. It is a weighted code.
Self-complementary It is not self-complementary It is self-complementary. It is self-complementary.
Sequential It is sequential. It is not sequential It is not sequential

Key Points to Remember

Here are the key points to remember in “Binary Coded Decimal (BCD)”.

  • Codes are used to represent numbers as encodings of binary bits (1s and 0s).
  • Codes can be weighted/non-weighted, self-complementary and sequential/non-sequential.
  • BCD stands for Binary Coded Decimal. It is used to encode the decimal digits from 0-9 as four-binary bits in terms of its positional values of 8-4-2-1.
  • For weighted code, the sum of positional values which have 1 as its encoding will give the equivalent decimal digit.
  • BCD codes can be interconverted into decimal and binary.
  • BCD numbers having groups of bits in between 0000-1001 are valid only.
  • BCD code is not complementary but sequential.
  • 2-4-2-1 and 8-4 -(-2) -(-1) are the two other weighted, self-complementary, and non-sequential codes.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.