Converting To and From Gray Code
The following functions in C convert between binary numbers and their associated Gray codes.
/* The purpose of this function is to convert an unsigned binary number to reflected binary Gray code. The operator >> is shift right. The operator ^ is exclusive or. */ unsigned int binaryToGray(unsigned int num) { return (num >> 1) ^ num; } /* The purpose of this function is to convert a reflected binary Gray code number to a binary number. */ unsigned int grayToBinary(unsigned int num) { unsigned int numBits = 8 * sizeof(num); unsigned int shift; for (shift = 1; shift < numBits; shift = 2 * shift) { num = num ^ (num >> shift); } return num; }Read more about this topic: Gray Code
Famous quotes containing the words converting, gray and/or code:
“A way of certifying experience, taking photographs is also a way of refusing itby limiting experience to a search for the photogenic, by converting experience into an image, a souvenir. Travel becomes a strategy for accumulating photographs.”
—Susan Sontag (b. 1933)
“The immense majority of human biographies are a gray transit between domestic spasm and oblivion.”
—George Steiner (b. 1929)
“...I had grown up in a world that was dominated by immature age. Not by vigorous immaturity, but by immaturity that was old and tired and prudent, that loved ritual and rubric, and was utterly wanting in curiosity about the new and the strange. Its era has passed away, and the world it made has crumbled around us. Its finest creation, a code of manners, has been ridiculed and discarded.”
—Ellen Glasgow (18731945)