CleverSphere

Gray code and BCD

Gray code changes only one bit from one number to the next, BCD encodes each decimal digit in four bits.

Rules

  • Gray = x ⊕ (x >> 1)
  • Neighbours differ in one bit
  • BCD: four bits per decimal digit

Your turn

Three problems on this topic. Spaces don’t matter.

  1. 1 · Gray code & BCD
  2. 2
  3. 3

Write the decimal number in BCD.

636

BCD (groups of four)

Worked example

Convert the decimal number to Gray code (8 bits).

128

Worked solution

  1. 1Binary: 10000000
  2. 2Shift right once and XOR: 10000000 ⊕ 01000000
  3. 3= 11000000

Typical mistake: Neighbours in Gray code differ in exactly one bit.

Common questions

Where is Gray code used?

In rotary encoders and Karnaugh maps, because only one bit changes per step.

Related

More topics