EngineerPlay

Decimal to BCD and Gray code converter

47 is 0010 1111 in plain binary, 0011 1000 in Gray code and 0100 0111 in 8421 BCD. Gray code keeps the top bit and XORs each pair of neighbouring binary bits; BCD writes each decimal digit in its own 4 bits.

Your values

Result

BIN plain binary
0010 1111
GRAY Gray code
0011 1000
BCD 8421 BCD
0100 0111

Worked steps

  1. 47 = 32 + 8 + 4 + 2 + 1 → 0010 1111₂
  2. g = b XOR (b >> 1): 0010 1111 → 0011 1000
  3. 4 → 0100, 7 → 0111 → 0100 0111

Formulas used

The same formulas as the formula sheet and the practice questions; the worked steps print them with your numbers.

Questions

How do you convert binary to Gray code?

Keep the first bit, then write each next bit as the XOR of that binary bit and the one to its left: 1 where they differ, 0 where they match. 101111 becomes 111000.

What is the difference between BCD and binary?

Binary writes the whole number in base 2: 47 is 101111. BCD writes each decimal digit in its own group of 4 bits: 4 is 0100 and 7 is 0111, so 47 is 0100 0111.

Why is Gray code used in position encoders?

Neighbouring Gray codes differ in one bit only, so a reading taken just as the disk moves from one position to the next is never more than one position out.