4bit典型格雷码转换

4bit典型格雷格转换公式:

二进制:b3 b2 b1 b0

格雷码:g3 g2 g1 g0

g0 = b1^b0

g1 = b2^b1

g3 = b3^b2

g4 = 0^b3

 

转换函数

int convert(int n) {
	int bPre;
	int bCur;
	int nGray = 0;

	n	&= 0x0F;
	for (int i=0; i<4; ++i)
	{
		bPre	= n&(1<<(i+1));
		bPre	>>= 1;
		bCur	= n&(1<<(i+0));
		if (bPre != bCur) nGray += (1<<i);
	}

	return nGray;
}


 

你可能感兴趣的:(格雷码)