是否有内置的 Gray code .NET 框架中任何地方的数据类型?或者灰色和二进制之间的转换实用程序?我可以自己做,但如果轮子已经被发明了......
最佳答案
使用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;
}
关于c# - .NET 中的格雷码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1691074/