70,037
社区成员
发帖
与我相关
我的任务
分享unsigned short swap_byte(unsigned short i)
{
return ((i<<8)&0xFF00)¦((i>>8)&0x00FF);
}#include <stdio.h>
union swap
{
struct
{
unsigned int h:8;
unsigned int l:8;
};
unsigned short t;
};
int main()
{
union swap a;
a.t = 0x1234;
a.h ^= a.l;
a.l ^= a.h;
a.h ^= a.l;
printf("%x", a.t);
return 0;
}
return (((i >> 8) & 0x00ff) | ((i << 8) & 0xff00));
这样也可以
unsigned short swap_byte_3(unsigned short i)
{
unsigned char* p1 = (unsigned char*)&i;
unsigned char* p2 = p1 + 1;
unsigned char t = *p1;
(*p1) = (*p2);
(*p2) = t;
return i;
}