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

xiadun 2014-01-02 02:48:45
有一个32位整数如:65606
如何表示它的高2字节和低2字节
...全文
232 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiandingzhe 2014-01-02
  • 打赏
  • 举报
回复

union
{
  int32_t int_member;
  uint8_t char_member[4];
}
然后搞个宏判断大小端。
lm_whales 2014-01-02
  • 打赏
  • 举报
回复
引用 2 楼 turingo 的回复:
unsigned int a = 56505; unsigned short int b = (a >> 16) & 0xffff; //高2字节 unsigned short int c = a & 0xffff; //低2字节 [quote=引用 楼主 xiadun 的回复:] 有一个32位整数如:65606 如何表示它的高2字节和低2字节
[/quote] ++ 地址要分大小端 简单的移位运算,就不需要了分大小端。 平台自己会处理好这个问题的。
赵4老师 2014-01-02
  • 打赏
  • 举报
回复
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]);//大端内存
u012997273 2014-01-02
  • 打赏
  • 举报
回复
引用 2 楼 turingo 的回复:
unsigned int a = 56505; unsigned short int b = (a >> 16) & 0xffff; //高2字节 unsigned short int c = a & 0xffff; //低2字节 [quote=引用 楼主 xiadun 的回复:] 有一个32位整数如:65606 如何表示它的高2字节和低2字节
[/quote] unsigned int a = 56505; unsigned short int b = *((unsigned short*)&a); //高2字节 unsigned short int c = *(((unsigned short*)&a) + 1); //高2字节
图灵狗 2014-01-02
  • 打赏
  • 举报
回复
unsigned int a = 56505; unsigned short int b = (a >> 16) & 0xffff; //高2字节 unsigned short int c = a & 0xffff; //低2字节
引用 楼主 xiadun 的回复:
有一个32位整数如:65606 如何表示它的高2字节和低2字节
Pump天天学习 2014-01-02
  • 打赏
  • 举报
回复
要分大小端吧

70,024

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧