Gray Code
Objectives
We would like to :- Understand gray code.
Notes
- Reference Wikipedia
- Gray code is named after Frank Gray
- The code is used for
- Error correction in communications
- Analog to digital conversion.
- Genetic algorithms
- K-Maps
- And any time we want to count, but only change one bit at a time.
- It is "reflective code" because the top half and bottom half of tables are reflected.
- Two bit gray code
Value 1 0 0 0 0 1 0 1 2 1 1 3 1 0 - Note bit 1: goes 0011
- Bit 0: 01 is reflected to 10
- Note that each number, only changes by one bit.
- Two bit gray code
- Let's try three bit.
-
Value 2 1 0 0 0 0 0 1 0 0 1 2 0 1 1 3 0 1 0 4 1 1 0 5 1 1 1 6 1 0 1 7 1 0 0
-
- Build the table for 4 bit gray code.
-
Value 3 2 1 0 0 0 0 0 0 1 0 0 0 1 2 0 0 1 1 3 0 0 1 0 4 0 1 1 0 5 0 1 1 1 6 0 1 0 1 7 0 1 0 0 8 1 1 0 0 9 1 1 0 1 10 1 1 1 1 11 1 1 1 0 12 1 0 1 0 13 1 0 1 1 14 1 0 0 1 15 1 0 0 0
-
- Conversion from binary to gray code:
- To convert from binary
- The MSB is the same
- Gmsb = Bmsb
- Every other bit xor with the previous binary bit.
- Gn-1 = Bn ⊕ Bn-1
-
Convert 13 to gray code 11012 = G3 = B3 = 1 G2 = B3 ⊕ B2 = 1 ⊕ 1 = 0 G1 = B2 ⊕ B1 = 1 ⊕ 0 = 1 G0 = B1 ⊕ B0 = 0 ⊕ 1 = 1 = 1011gray Convert 10 to gray code 10102 G3 = B3 = 1 G2 = B3 ⊕ B2 = 1 ⊕ 0 = 1 G1 = B2 ⊕ B1 = 0 ⊕ 1 = 1 G0 = B1 ⊕ B0 = 1 ⊕ 0 = 1 = 1111gray
- The MSB is the same
-
- To convert from binary
- Convert from Gray to Binary
- BMSB = GMSB
- Bn-1 = Bn ⊕ Gn-1
-
Convert 1001gray to binary 1001gray = B3 = G3 = 1 B2 = B3 ⊕ G2 = 1 ⊕ 0 = 1 B1 = B2 ⊕ G1 = 1 ⊕ 0 = 1 B0 = B1 ⊕ G0 = 1 ⊕ 1 = 0 = 11102 -
- the digital file
- I tested them with:
-
- this file