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转换为二进制 /* 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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |