[牛人杂谈] 大小端应该看高低位字节的顺序

[复制链接]
647|7
 楼主| 幸福小强 发表于 2020-2-25 21:51 | 显示全部楼层 |阅读模式
大小端应该看的是高低位字节的顺序,而不是位的顺序。
  1. #include"stdio.h"
  2. typedef struct
  3. {
  4.         unsigned char b;
  5.         unsigned char c;
  6. }tow;

  7. typedef union TWO
  8. {
  9. unsigned int a;
  10. tow yy;
  11. };
  12. void main(void)
  13. {
  14.         union TWO xx;

  15.         xx.a=0x3521;

  16.        
  17.         while(1);
  18. }

微信图片_20200225215021.png
高字节出现在了低位地址的变量。
 楼主| 幸福小强 发表于 2020-2-25 21:51 | 显示全部楼层
因此这是大端模式。
而在字节内的位则是按顺序高位在高地址,低位在低地址。。
 楼主| 幸福小强 发表于 2020-2-25 21:57 | 显示全部楼层
  1. #include"stdio.h"
  2. typedef struct
  3. {
  4.         unsigned char b1:1;
  5.         unsigned char b2:1;
  6.         unsigned char b3:1;
  7.         unsigned char b4:1;
  8.         unsigned char c:4;       
  9. }tow;

  10. typedef union TWO
  11. {
  12. unsigned char a;
  13. tow yy;
  14. };
  15. void main(void)
  16. {
  17.         union TWO xx;

  18.         xx.a=0x35;

  19.        
  20.         while(1);
  21. }


406875e5527cce74df.png
 楼主| 幸福小强 发表于 2020-2-25 21:58 | 显示全部楼层
可以看出来位是小端在前的。而字节是大端在前。。
有何不可0365 发表于 2020-2-25 23:00 | 显示全部楼层
 楼主| 幸福小强 发表于 2020-3-9 20:41 | 显示全部楼层
回头看看,发帖很有必要,现在都忘了。
antusheng 发表于 2020-3-12 10:02 | 显示全部楼层
总结的很好。
wahahaheihei 发表于 2020-3-15 21:50 | 显示全部楼层
莫非可以直接在Keil使用uchar作为类型关键字
您需要登录后才可以回帖 登录 | 注册

本版积分规则

143

主题

1720

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部