# BCD to Gray code converter

The task is to design a bcd to gray code converter. The 5-bit bcd input is represented as A,B,C,D,E while the 4-bit gray code output as D3, D2, D1 & D0.

Could someone help me confirm my truth table? • Gray code has one bit change per count. What will happen yours when you rollover from 9 to 0? – Transistor Jul 30 at 10:37
• This is correct, stop posting homework :'( – Sorenp Jul 30 at 10:39
• thanks for the confirmation. I won't bother posting this if it was your average "homework". @Transistor I see what you are pointing out. In my table, to rollover from 9 to 0 will require 3 bit change and that is impossible for gray code. Any ideas what to do? Thanks – Peanuts Jul 30 at 10:59
• @Peanuts: Yes, I have ideas. What are the possible Gray (note capital) codes for '9'? Which of these satisfy the 1-bit change requirement on rollover from 8 to 9 and 9 to 0? – Transistor Jul 30 at 11:26
• @Transistor I just did some reading up on this. I found out that an alternative notation of 9 in Gray code is 1000. This satisfies the 1-bit change requirement. But I don't get the reasoning behind it. Could you explain that? I mean, coz 1000 is 15 in gray code so I'm quite confused how it can be 9 at the same time. – Peanuts Jul 30 at 11:40

[Transistor] Gray code has one bit change per count. What will happen yours when you rollover from 9 to 0?

[Peanuts] I see what you are pointing out. In my table, to rollover from 9 to 0 will require 3 bit change and that is impossible for gray code.

Correct. Now, what are the possible Gray (note capital) codes for '9'? Which of these satisfy the 1-bit change requirement on rollover from 8 to 9 and 9 to 0?

[Peanuts] I found out that an alternative notation of 9 in Gray code is 1000. This satisfies the 1-bit change requirement. But I don't get the reasoning behind it. Could you explain that? I mean, coz 1000 is 15 in gray code so I'm quite confused how it can be 9 at the same time.

1000 is correct.

b1000 is d15 in one particular implementation of a Gray code. You have been asked to develop your own. You didn't have to, for example, make the first change on bit 0. Since whatever you use to interpret the Gray code requires a lookup table you are free to order the bits in any sequence such that two successive values differ in only one bit.

The Gray code is generally generated as a binary-reflected code.

Table 1. 4-bit Gray code.

0000   0
0001   1
0011   2
0010   3
0110   4
0111   5
0110   6
0100   7
1100   8
1101   9
1111  10
1110  11
1010  12
1011  13
1001  14
1000  15


Draw a line between rows 7 and 8. Can you see that the pattern of the other bits is reflected above and below the line? Do the same between 3 and 4. Can you see that the last two bits are reflected above and below the line?

Table 2. OP's solution.

0000   0
0001   1
0011   2
0010   3
0110   4
0111   5
0110   6
0100   7
1100   8
1000   9


Now notice that your solution of using 1000 for '9' breaks the reflection. Can you modify your Gray code to work for BCD while retaining the reflection? (I'm only able to do it for the most significant bit. I suspect that's as good as it gets because 10 is not an integer power of 2.)

The MSB in your question is always zero so it can be ignored.

• Good catch on the roll-over. – Mattman944 Jul 30 at 13:12