# Gray Code Data

A Gray code (after the inventer Frank Gray) is any binary coding sequence in which only a single bit position changes as we move from one value to the next.

E.g. in normal binary counting, when you go from 1 to 2, the binary values are 01, then 10. In that case, both bits changed. The lower 1 changed to a 0 and the upper (implied) 0 changed to a 1. In Gray code, 1 is 01, but 2 is 11 (only the 2nd bit changed) and then the binary pattern 10 is used for 3 instead (only the lower bit changes from 2 to 3)

Gray Codes are often used in Input sensors for position change, Grey Code / Single Track Grey Code Encoders because it is impossible to ensure that two bit sensors in an encoder will always change value at exactly the same time.

Here are the values from 0 to 15 in Gray Code Binary encoding:

 Value Binary Gray Encoding 0 0000 0000 1 0001 0001 2 0010 0011 3 0011 0010 4 0100 0110 5 0101 0111 6 0110 0101 7 0111 0100 8 1000 1100 9 1001 1101 10 1010 1111 11 1011 1110 12 1100 1010 13 1101 1011 14 1110 1001 15 1111 1000

There are many such codes, but the traditional one is computed such that the Kth Gray code is K^(K>>1). The ^ operator is XOR, in this case, it means that the K>>1'th bit in K is toggled. >> is the shift right operator, which in this case is shiving the value one bit to the right. In short, K^(K>>1), means that to fined each value, start with the binary version of that value, then shift it right one binary digit and XOR that with the original.

The well-known algorithm for conversion from Gray to binary is a linear sequence of XORs that makes it seem each bit must be dealt with separately. Fortunately, that is equivalent to a parallel prefix XOR that can be computed using SWAR* techniques in log time. For 32-bit Gray code values produced as described above, the conversion from Gray code back to unsigned binary is:

```	unsigned int
g2b(unsigned int gray) {
gray ^= (gray >> 16);
gray ^= (gray >> 8);
gray ^= (gray >> 4);
gray ^= (gray >> 2);
gray ^= (gray >> 1);
return(gray);
}
```

* SWAR (SIMD Within A Register) is a technique for performing parallel operations on data contained in a processor register. SIMD (Single Instruction, Multiple Data) is the application of a single operator to multiple data values in parallel. E.g. XOR operation applied to two byte wide values. All the bits are processed at once.

 file: /Techref/graycodes.htm, 4KB, , updated: 2015/11/2 16:17, local time: 2018/11/18 06:16, TOP NEW HELP FIND:  54.144.100.123:LOG IN

 ©2018 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?Please DO link to this page! Digg it! / MAKE! /  Gray Code Data

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant" "No. But I'm interested. me at when this page is expanded."

### Welcome to sxlist.com!

Site supported by
& kind contributors
just like you!

(here's why

Copies of the site on CD
are available at minimal cost.

.