I am doing one of the tutorial in CS2100 in **NUS** SoC and one of the questions require us to convert between binary and gray codes.

I have decided to write a simple tutorial on how to convert from binary to gray codes.

**What is Gray Code?**

*“….is a binary numeral system where two successive values differ in only one bit. The reflected binary code was originally designed to prevent spurious output from electromechanical switches.”*

~http://en.wikipedia.org/wiki/Gray_code

**What is Binary Code?**

*A binary code represents text or computer processor instructions using the binary number system’s two binary digits, 0 and 1.*

~http://en.wikipedia.org/wiki/Binary_code

Before we start, lets look at an Exclusive OR, XOR(sometimes EOR gate, or EXOR gate)’s truth table.

The whole concept on how to do the conversion between the 2 number system heavily uses the XOR truth table.

Binary to Gray Code Conversion : Step 1 – Prepare a table like this

Binary to Gray Code Conversion : Step 2 – Copy down the Most Significant Bit (MSB) of Binary onto Gray Code row

Binary to Gray Code Conversion : Step 3 – Get the MSB and next MSB of Binary and XOR them and copy its result down

Binary to Gray Code Conversion : Step 4 – Get the MSB and next MSB of Binary and XOR them and copy its result down

Binary to Gray Code Conversion : Step 5 – Get the MSB and next MSB of Binary and XOR them and copy its result down

Binary to Gray Code Conversion : Step 6 – Get the MSB and next MSB of Binary and XOR them and copy its result down

Binary to Gray Code Conversion : Step 7 – Get the MSB and next MSB of Binary and XOR them and copy its result down

Binary to Gray Code Conversion : Step 8 – Get the MSB and next MSB of Binary and XOR them and copy its result down

Gray Code to Binary Conversion : Step 1 – Prepare a table like this

Gray Code to Binary Conversion : Step 2 – Copy up the Most Significant Bit (MSB) of Gray Code onto Binary row

Gray Code to Binary Conversion : Step 3 – Take the MSB of Binary and next most MSB of Gray Code and XOR them. Copy its result upwards.

Gray Code to Binary Conversion: Step 4 – Take the MSB of Binary and next most MSB of Gray Code and XOR them. Copy its result upwards.

Gray Code to Binary Conversion: Step 5 – Take the MSB of Binary and next most MSB of Gray Code and XOR them. Copy its result upwards.

Gray Code to Binary Conversion: Step 6 – Take the MSB of Binary and next most MSB of Gray Code and XOR them. Copy its result upwards.

Gray Code to Binary Conversion: Step 7 – Take the MSB of Binary and next most MSB of Gray Code and XOR them. Copy its result upwards.

Gray Code to Binary Conversion: Step 8- Take the MSB of Binary and next most MSB of Gray Code and XOR them. Copy its result upwards.

*Related*

## No Comments