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
Enter a whole number from 0 to 9,999.
These values have no real result.
Result
- BIN plain binary
- GRAY Gray code
- BCD 8421 BCD
Fix the value marked above to see the result.
Worked steps
- 47 = 32 + 8 + 4 + 2 + 1 → 0010 1111₂
- g = b XOR (b >> 1): 0010 1111 → 0011 1000
- 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.
gᵢ = bᵢ XOR bᵢ₊₁N = d₃ × 1000 + d₂ × 100 + d₁ × 10 + d₀
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.