A Gray code is a sequence of bit patterns in which any two adjacent patterns in the
Question:
A Gray code is a sequence of bit patterns in which any two adjacent patterns in the sequence differ by only one bit. Gray codes were developed to aid in error correction of mechanical switches but are used more generally in digital systems. For example, for four bits, the first six values in the Gray code are: 0000, 0001, 0011, 0010, 0110, 0111. The rule to convert a binary number n into the corresponding Gray code g is to move bit by bit, from lowest bit to highest bit, setting the output bit to the original bit or inverted if the next higher bit is 1. So, to change n = 0011 to g = 0010 we output a 0 for bit 0 of g since bit 1 of n is set and output the existing bit value of n for all other positions since the next higher bit is not set.
Using this rule, write a C function that calculates the Gray code g for any unsigned short n input.
Step by Step Answer: