分类: LINUX
2016-03-13 21:59:24
#define sw16(x) \
((short)( \
(((short)(x) & (short)0x00ffU) << 8 ) | \
(((short)(x) & (short)0xff00U) >> 8 ) ))
#define sw16(x) \
((uint16_t)( \
(((uint16_t)(x) & (uint16_t)0x00ff) << 8 ) | \
(((uint16_t)(x) & (uint16_t)0xff00) >> 8 ) ))
#define sw32(x) \
((((uint32_t)(x) & 0xff000000) >> 24) | \
(((uint32_t)(x) & 0x00ff0000) >> 8) | \
(((uint32_t)(x) & 0x0000ff00) << 8) | \
(((uint32_t)(x) & 0x000000ff) << 24))
假设x=0xaabb
(short)(x) & (short)0x00ffU) 与将16位数高8位置0 成了0x00bb 然后<<8 向左移8位后 低8位变成了高8位 低8位补0 结果为 0xbb00
(((short)(x) & (short)0xff00U) >> 8 ) )) 恰好相反 得到的结果为 0x00aa
两个结果再 或一下 0xbb00 | 0x00aa 就成了 0xbbaa
转换函数
static u16 le16_to_cpu(u16 data)
{
int x = 1;
u8 p;
if(*(char *)&x == 1)
return data; //little endian
else
p = (u8)(data & 0xff);
return (((data & 0xff00) >> 8) | (p <<8)); //big-endian
}