加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – .NET中的格雷码

发布时间:2020-12-15 18:13:40 所属栏目:百科 来源:网络整理
导读:.NET框架中的任何地方都有内置的 Gray code数据类型吗?或者Gray和binary之间的转换实用程序?我可以自己做,但如果轮子已经发明了…… 解决方法 使用 this trick. /* The purpose of this function is to convert an unsigned binary number to reflected bi
.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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读