首页 > 其他 > 详细

Convertion of grey code and binary 格雷码和二进制数之间的转换

时间:2015-03-05 14:35:36      阅读:291      评论:0      收藏:0      [点我收藏+]

 

以下转换代码摘自维基百科 Wikipedia:

 

/*
        The purpose of this function is to convert an unsigned
        binary number to reflected binary Gray code.
 
        The operator >> is shift right. The operator ^ is exclusive or.
*/
unsigned int binaryToGray(unsigned int num)
{
        return (num >> 1) ^ num;
}
 
/*
        The purpose of this function is to convert a reflected binary
        Gray code number to a binary number.
*/
unsigned int grayToBinary(unsigned int num)
{
    unsigned int mask;
    for (mask = num >> 1; mask != 0; mask = mask >> 1)
    {
        num = num ^ mask;
    }
    return num;
}

 

Convertion of grey code and binary 格雷码和二进制数之间的转换

原文:http://www.cnblogs.com/grandyang/p/4315607.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!