读书人

示意整数的高2字节和低2字节

发布时间: 2014-01-15 15:40:23 作者: rapoo

表示整数的高2字节和低2字节
本帖最后由 xiadun 于 2014-01-02 14:52:32 编辑 有一个32位整数如:65606
如何表示它的高2字节和低2字节
[解决办法]
unsigned int a = 56505;
unsigned short int b = (a >> 16) & 0xffff; //高2字节
unsigned short int c = a & 0xffff; //低2字节

引用:
有一个32位整数如:65606
如何表示它的高2字节和低2字节

[解决办法]
引用:
unsigned int a = 56505;
unsigned short int b = (a >> 16) & 0xffff; //高2字节
unsigned short int c = a & 0xffff; //低2字节

Quote: 引用:

有一个32位整数如:65606
如何表示它的高2字节和低2字节

unsigned int a = 56505;
unsigned short int b = *((unsigned short*)&a); //高2字节
unsigned short int c = *(((unsigned short*)&a) + 1); //高2字节
[解决办法]
int i=65606;
printf("%04X %04X",((unsigned short *)&i)[1],((unsigned short *)&i)[0]);//小端内存
printf("%04X %04X",((unsigned short *)&i)[0],((unsigned short *)&i)[1]);//大端内存

[解决办法]
引用:
unsigned int a = 56505;
unsigned short int b = (a >> 16) & 0xffff; //高2字节
unsigned short int c = a & 0xffff; //低2字节

Quote: 引用:

有一个32位整数如:65606
如何表示它的高2字节和低2字节

++

地址要分大小端
简单的移位运算,就不需要了分大小端。
平台自己会处理好这个问题的。

[解决办法]

union
{
int32_t int_member;
uint8_t char_member[4];
}

然后搞个宏判断大小端。

读书人网 >C语言

热点推荐