c# - .NET 中的格雷码

标签 c# .net error-checking gray-code

是否有内置的 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/

相关文章:

c# - 在拖放过程中使控件对拖动事件透明

javascript - 在提交之前检查 javascript 中的 django 表单错误的最佳方法是什么?

excel - 如何使用 VBA 忽略范围内的绿色三角形错误,而不逐个单元循环?

c# - 重写 .NET WebBrowsers WndProc

c# 对象初始值设定项的复杂性。最佳实践

c# - 从 app.config 加载自定义部分时配置系统无法初始化错误

c - 为什么我们在用于内存测试的模式末尾找到 ULL?

c# - .NET 的 Random() 的实现更改

.net - 为什么.NET中8号字体显示为8.25?

c# - DhtmlX 甘特图未获得正确的链接目标 ID