一个二进制数转为格雷码的小程序
/*这是一个二进制数转为格雷码的小程序,此计算方法特*/
/*别简单实用,下面以16位双字节为例。 */
/***************************************************/
/* 二进制码转换程序->格雷码 */
unsigned int hextorel(unsigned int rel); */
/* */
/***************************************************/
/* hextorel()为:二进制码->格雷码转换程序 */
/* 入口为:hex,出口为:函数的返回值 */
/* 计算公式如下: */
/* CnCn-1Cn-2......C1C0 */
/* ⊕ CnCn-1Cn-2''''C2c1 */
/* ---------------------- */
/* RnRn-1Rn-2......R2R1 */
/* */
/* */
/* CnCn-1Cn-2......C1C0----二进制码 */
/* RnRn-1Rn-2......R2R1----各类码 */
/* ⊕----异或 */
/***************************************************/
unsigned int bdata hex_code; /*二进制数*/
unsigned int bdata rel_code; /*格雷码 */
/***************************************************/
unsigned int hextorel(unsigned int hex)
{
hex_code=hex;
hex_code=hex_code^(hex_code>>1);
return hex_code;
}
/***************************************************/
/*这是一个二进制数转为格雷码的小程序,此计算方法特*/
/*别简单实用,下面以16位双字节为例。 */
/***************************************************/
/* 二进制码转换程序->格雷码 */
unsigned int hextorel(unsigned int rel); */
/* */
/***************************************************/
/* hextorel()为:二进制码->格雷码转换程序 */
/* 入口为:hex,出口为:函数的返回值 */
/* 计算公式如下: */
/* CnCn-1Cn-2......C1C0 */
/* ⊕ CnCn-1Cn-2''''C2c1 */
/* ---------------------- */
/* RnRn-1Rn-2......R2R1 */
/* */
/* */
/* CnCn-1Cn-2......C1C0----二进制码 */
/* RnRn-1Rn-2......R2R1----各类码 */
/* ⊕----异或 */
/***************************************************/
unsigned int bdata hex_code; /*二进制数*/
unsigned int bdata rel_code; /*格雷码 */
/***************************************************/
unsigned int hextorel(unsigned int hex)
{
hex_code=hex;
hex_code=hex_code^(hex_code>>1);
return hex_code;
}
/***************************************************/