C语言 LSB->MSB,MSB->LSB转换

32位整型数据bit位高低转换

unsigned int reverse(register unsigned int x)
{
	x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
	x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
	x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
	x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
	return((x >> 16) | (x << 16));

}

16位整型数据bit位高低转换

unsigned short reverse(unsigned short x)
{
	x = (((x & 0xaaaa) >> 1) | ((x & 0x5555) << 1));
	x = (((x & 0xcccc) >> 2) | ((x & 0x3333) << 2));
	x = (((x & 0xf0f0) >> 4) | ((x & 0x0f0f) << 4));

	return((x >> 8) | (x << 8));

}

8位整型数据bit位高低转换

uint8_t reverse(uint8_t x)
{
    x = (((x & 0xaa) >> 1) | ((x & 0x55) << 1));  
    x = (((x & 0xcc) >> 2) | ((x & 0x33) << 2));  
    return((x >> 4) | (x << 4));  
}

你可能感兴趣的:(c语言,stm32,嵌入式,算法)