Mike Halliday <spamBeGonePICLISTspamBeGone
MITVMA.MIT.EDU> wrote:
> Gray code should be generated by Gray = binary xor (binary * 2)
Mike:
This is ALMOST correct. Gray code is generated by:
Gray = binary xor (binary / 2)
That is, you must shift the input number RIGHT one position
before XORing it with the original number.
{Quote hidden}> But binary is extracted as follows:-
>
> For n the nth bit of n bits, B being the binary output bit, and G
> the Gray input bit
>
> 1) B(n) = G(n)
> 2) B(n-1) = G(n-1) xor B(n)
> 3) B(n-2) = G(n-2) xor B(n-1)
> 4) B(n-3) = G(n-3) xor B(n-2)
> ......
> 5) B(0) = G(0) xor B(1)
Another way of looking at it, which may be easier to implement,
is:
1. Reading from left to right, accept all the "0"'s and the
first "1".
2. INVERT = TRUE.
3. Read the next bit. If INVERT is true, invert that bit.
Otherwise, accept it as-is.
4. If the bit you just read was a "0", loop back to Step 3.
5. Otherwise, invert the state of the INVERT flag, then loop
back to Step 3.
-Andy
Andrew Warren - TakeThisOuTfastfwdEraseME
spam_OUTix.netcom.com
Fast Forward Engineering, Vista, California
http://www.geocities.com/SiliconValley/2499