摘自维基百科的转换代码:
/*
The purpose of this function is to convert an unsingned
binary number to reflected binary Grey code.
*/
unsigned int binary2Grey(unsigned int num){
return (num>>1)^num;
}
/*
The purpose of this function is to convert a reflected binary
Grey code to a binary number.
*/
unsigned int grey2Binary(unsigned int num){
unsigned int mask;
for(mask = num>>1;mask!=0;mask>>=1){
num^=mask;
}
return num;
}