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 wellknown 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 32bit 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.
See also:
file: /Techref/graycodes.htm, 4KB, , updated: 2015/11/2 17:17, local time: 2018/8/21 03:28,

©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? <A HREF="http://www.sxlist.com/techref/graycodes.htm"> Gray Code Data</A> 
Did you find what you needed? 
Welcome to sxlist.com!sales, advertizing, & kind contributors just like you! Please don't rip/copy (here's why Copies of the site on CD are available at minimal cost. 
Welcome to www.sxlist.com! 
.