please dont rip this site

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
 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);

* 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: 2024/7/18 11:33,

 ©2024 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!

<A HREF=""> Gray Code Data</A>

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 a nice message (short messages are blocked as spam) 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.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

  PICList 2024 contributors:
o List host: MIT, Site host, Top posters @none found
- Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.

Welcome to!