Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Binary Coded Decimal

Status
Not open for further replies.

PG1995

Active Member
Hi

I'm learning about BCD (Binary Coded Decimal). I'm learning BCD 8421 code. I'm mentioning this because this might useful to someone like me to knoe that there are many ways to represent a decimal into a binary forms but the BCD most commonly refers to 8421 code.

Q1: I was trying to see how the conversion from Decimal to Binary takes place 'rationally'.

14 = 0001 0100
14 = (1 x 10^1) + (4 x 10^0)
14 = 1 x (2 x 5) + (2^2 x 2^0)
14 = 1 x (2 x (4+1) + 2^2
14 = 2(2^2 + 2^0) + 2^2
14 = 2^3 + 2^1 + 2^2
14 = 2^3 + 2^2 + 2^1 + 0
14 = 1110

Now how do I change the number "1110" into a BCD one without directly using cookbook approach of adding '6' (0110) to the result? My question #2 is also related to this.

Q2: Please have a look on the attachment. You can find my query there. Thanks.

Thank you very much for the help and your time.

Regards
PG
 

Attachments

  • Floyd - Digital Fundamentals 9e_0101.jpg
    Floyd - Digital Fundamentals 9e_0101.jpg
    321.5 KB · Views: 494
Last edited:
BCD = Binary Coded Decimal
ie Decimal numbers, numbers in the range 1 to 9 converted into a binary form.
Look at the table below:
Code:
Binary(4bits)    Decimal       Hexadecimal      BCD
0000                  0             0            0
0001                  1             1            1
0010                  2             2            2
0011                  3             3            3
0100                  4             4            4
0101                  5             5            5
0110                  6             6            6
0111                  7             7            7
1000                  8             8            8
1001                  9             9            9
1010                 10             A         Not Valid
1011                 11             B         Not Valid
1100                 12             C         Not Valid
1101                 13             D         Not Valid
1110                 14             E         Not Valid
1111                 15             F         Not Valid

Using 4 bits we can count up to 15, in BCD we can only count up to 9, numbers 10 to 15 should not, cannot, must not, exist as a BCD digit.

Consider the decimal number 2793, to convert this to BCD, just look at each digit
2 = 0010
7 = 0111
9 = 1001
3 = 0011
So, 2793 is 0011 0111 1001 0011 in BCD.

If you have a binary number 1110, this is an invalid BCD number and has the decimal value 14,
so the BCD equivalent would be 14 = 0001 0100
1 = 0001
4 = 0100

Let me just emphasise 1110 is not a valid BCD number, but it is a binary number so just convert the binary number to a decimal number and then you can convert decimal to BCD by a simple inspection process.

As far as adding "6" goes, to me that feels like a bit of a confuser, but is does work.

Code:
  1110 +
  0110
 ------
1 0100

Your text book also write some BCD numbers as 1001000001000011 or some such nonsense.
Again I think this is just plain confusing.
BCD is groups of four bits each representing a decimal digit, so why dont they write it as
1001 0000 0100 0011 ???

But then when did anything in a school textbook have to be logical?
Just my distorted opinion.

As a side issue, BCD is usually in 1248 form, but there are others such as 1245 BCD and Grey Code, and others.
Grey Code is quite common, but I cannot remember seeing 1245 used, but then that is just me.

Does this help?

JimB
 
Last edited:
Jimb,

As a side issue, BCD is usually in 1248 form, but there are others such as 1245 BCD and Grey Code, and others.
Grey Code is quite common, but I cannot remember seeing 1245 used, but then that is just me.

The default definition of BCD is 8421 or the "natural binary" code. The Gray code (invented by Frank Gray) is a reflected binary code.

Ratch
 
The default definition of BCD is 8421 or the "natural binary" code.
I thought that I had said that, implicitly, if not explicitly spelled out.
When I learned BCD, the natural form was expressed as 1248 BCD, rather than 8421 BCD.

The Gray code (invented by Frank Gray) is a reflected binary code.
Yes of course, Mrs Grays little lad.
The advantage of Gray code is of course that only one bit changes from one state to the next, unlike the natural BCD where there can be up to three bits changing between states.

JimB
 
BCD = Binary Coded Decimal
ie Decimal numbers, numbers in the range 1 to 9 converted into a binary form.
Look at the table below:
Code:
Binary(4bits)    Decimal       Hexadecimal      BCD
0000                  0             0            0
0001                  1             1            1
0010                  2             2            2
0011                  3             3            3
0100                  4             4            4
0101                  5             5            5
0110                  6             6            6
0111                  7             7            7
1000                  8             8            8
1001                  9             9            9
1010                 10             A         Not Valid
1011                 11             B         Not Valid
1100                 12             C         Not Valid
1101                 13             D         Not Valid
1110                 14             E         Not Valid
1111                 15             F         Not Valid

Using 4 bits we can count up to 15, in BCD we can only count up to 9, numbers 10 to 15 should not, cannot, must not, exist as a BCD digit.

Consider the decimal number 2793, to convert this to BCD, just look at each digit
2 = 0010
7 = 0111
9 = 1001
3 = 0011
So, 2793 is 0011 0111 1001 0011 in BCD.

If you have a binary number 1110, this is an invalid BCD number and has the decimal value 14,
so the BCD equivalent would be 14 = 0001 0100
1 = 0001
4 = 0100

Let me just emphasise 1110 is not a valid BCD number, but it is a binary number so just convert the binary number to a decimal number and then you can convert decimal to BCD by a simple inspection process.

As far as adding "6" goes, to me that feels like a bit of a confuser, but is does work.

Code:
  1110 +
  0110
 ------
1 0100

Your text book also write some BCD numbers as 1001000001000011 or some such nonsense.
Again I think this is just plain confusing.
BCD is groups of four bits each representing a decimal digit, so why dont they write it as
1001 0000 0100 0011 ???

But then when did anything in a school textbook have to be logical?
Just my distorted opinion.

As a side issue, BCD is usually in 1248 form, but there are others such as 1245 BCD and Grey Code, and others.
Grey Code is quite common, but I cannot remember seeing 1245 used, but then that is just me.

Does this help?

JimB

Thanks a lot, JimB. I appreciate your help.

But that was the main problem. I wanted to know the rationale behind the addition of '6' to the result. There has to be a reason because internally math doesn't follow cookbook approach. Could you please help me with it? Thanks.

Regards
PG
 
pg1995,

But that was the main problem. I wanted to know the rationale behind the addition of '6' to the result. There has to be a reason because internally math doesn't follow cookbook approach. Could you please help me with it? Thanks.

You only have to worry about adding 6 to BCD numbers when you are adding them. For instance, if you add 7 + 5, you will get 12. Since 12 is not a valid single digit BCD, you add 6 and get a carry out to the next higher digit. 2 remains in the lower digit and is a valid BCD single digit number. So adding 6 gets the decimal number past the hex digits of A,B,C,D,E,F. If the BCD single digit is valid, then you do not add 6. Understand now?

Ratch
 
Thank you, Ratch.

But why isn't it "5" or "4" instead of "6" which can be added to the result if it's not in standard BCD code? Please help me with it. Thanks.
 
BCD uses four binary digits (bits) to represent the numbers 0 to 9.

However, four bits can represent up to 15 (decimal), so the four bits of our BCD number have six possible invalid states, as shown in the table in my post #3.

If we have the "BCD" number 1110, this number is an invalid BCD and cannot exist.
It is a valid binary or hexadecimal number equivalent to 14 in decimal.
To get the number back into a valid BCD format, we have to jump over the six hexadecimal numbers A to F, to do this we add 6.

Adding 6 to 14 (decimal) gives 20

Code:
      1110 +      14 decimal
      0110          6  decimal
   -------
     10100        20  decimal

So we end up with 10100 in binary which is 20 in decimal.
But, but, this is not really a binary number it is a BCD number, so separate the most significant bit and write it as 1 0100 and this is 14 in BCD.

I dont know if this helps, I confused myself a few times trying to write it out, but it does work.


An example in the form of an algorithm to add two single digit BCD numbers;

Let the numbers be A and B

Add A + B and put the result in C

If C <= 9 then answer is C

If C > 9 then there is an overflow

Add 6 to C, the answer is the 4 least significant bits of C, and carry 1 into the next BCD digit.

I hope this helps.

JimB
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top