/* Gray <==> binary conversion routines */
/* written by Dan T. Abell, 7 October 1993 */
/* please send any comments or suggestions */
/* to dabell@quark.umd.edu */
void gray_to_binary (Cg, Cb, n)
/* convert chromosome of length n+1 */
/* from Gray code Cg[0...n] */
/* to binary code Cb[0...n] */
allele *Cg, *Cb;
int n;
{
int j;
*Cb = *Cg; /* copy the high-order bit */
for (j = 0; j < n; j++)
{
Cb--;
Cg--; /* for the remaining bits */
*Cb = *(Cb+1) ^ *Cg; /* do the appropriate XOR */
}
}
void binary_to_gray(Cb, Cg, n)
/* convert chromosome of length n+1 */
/* from binary code Cb[0...n] */
/* to Gray code Cg[0...n] */
allele *Cb, *Cg;
int n;
{
int j;
*Cg = *Cb; /* copy the high-order bit */
for (j = 0; j < n; j++)
{
Cg--;
Cb--; /* for the remaining bits */
*Cg = *(Cb+1) ^ *Cb; /* do the appropriate XOR */
}
}
The above algorithm was found at: