.NET框架中的任何地方都有内置的
Gray code数据类型吗?或者Gray和binary之间的转换实用程序?我可以自己做,但如果轮子已经发明了……
解决方法
使用
this trick.
/* The purpose of this function is to convert an unsigned binary number to reflected binary Gray code. */ unsigned short binaryToGray(unsigned short num) { return (num>>1) ^ num; }
一个棘手的技巧:对于最多2 ^ n位,您可以将Gray转换为二进制
执行(2 ^ n) – 1次二进制到灰度转换.你所需要的只是
上面的函数和’for’循环.
/* The purpose of this function is to convert a reflected binary Gray code number to a binary number. */ unsigned short grayToBinary(unsigned short num) { unsigned short temp = num ^ (num>>8); temp ^= (temp>>4); temp ^= (temp>>2); temp ^= (temp>>1); return temp; }