码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-03-05 14:38:54      阅读:171      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!