> wfdavis@davis-inc.com wrote: > > > With regard to algorithms, here are two schemes. b(i) and g(i) > > refer to the i-th bit of the n-bit binary and Gray-code > > representations, respectively. The symbol X stands for exclusive > > OR. > > > > Binary to Gray-code conversion: > > > > g(i) = b(i) X b(i+1) where i = 0,1,2,...n-1 > > g(n) = b(n) > > Warren: > > This is wrong... Try it and you'll see. > > The CORRECT algorithm is g(i) = b(i) ^ (b(i) >> 1), where "^" > means "xor" and ">> 1" means "right-shifted 1 bit". Evidentally, you have misunderstood the notation. g(i) and b(i) refer to the i-th bits of the n-bit representations of g and b, respectively. This is why "i = 0,1,2, ... n-1" appears at the right. The formula must be applied n times, once for each of the values of i. When it is all done, all n bits of g will have been determined. The notation "b(i) >> 1" makes no sense; how does one shift a single bit right 1 bit unless one means to produce a consistent zero bit in the vacated single bit. I think what you have in mind and what you really meant to convey would be correctly notated as: g = b ^ (b >> 1) That is, the Gray-code equivalent of an entire n-bit binary number b is given by the exclusive OR of the original n-bit binary number and the same n-bit number shifted right 1 bit. Of course, it must be understood that a zero is to be shifted into the vacated position so that the high order bit of the result g is equal to the high order bit of b. This is covered by g(n) = b(n) in my notation. With the erroneous subscripts removed and the understanding about shifting in zero, your recipe and mine become equivalent. In any case, my original recipe is not incorrect as you claim. > > Gray-code to binary conversion: > > > > b(n) = g(n) > > b(i) = g(i) X b(i+1) where i = n-1,...2,1,0 > > This is also wrong, of course. With all due respect, this is likewise NOT incorrect. > > -Andy > --- Warren ================================================ Davis Associates, Inc. 43 Holden Road West Newton, MA 02165 U.S.A. Tel: 617-244-1450 FAX: 617-964-4917 Visit our web site at: http://www.davis-inc.com ================================================