有关8/16/32bits 数处理的宏 的理解

[复制链接]
2592|14
 楼主| 醉心369 发表于 2013-1-12 12:03 | 显示全部楼层 |阅读模式
本帖最后由 醉心369 于 2013-1-12 12:07 编辑

一组有关8/16/32bits 数处理的宏
typedef union
{
    UINT16 WordCode;
    struct
    {
        UINT16 _byte0 : 8; //LSB byte code
        UInt16 _byte1 : 8; //MSB byte code
     } ByteCode;
}UWORD;

typedef union
{
    INT16 WordCode;
    struct
    {
        UINT16 _byte0 : 8; //LSB byte code
        INT16 _byte1 : 8; //MSB byte code
    }ByteCode;
}SWORD;

typedef union
{
    UINT32 LongCode;
    struct
    {
        UINT16 _word0 :16; //LSB word code
        UINT16 _word1 :16; //MSB word code
    }WordCode;
    struct
    {
        UINT16 _byte0 :8; //bit 7 ~ 0
        UINT16 _byte1 :8; //bit 15 ~ 8
       UINT16 _byte2 :8; //bit 23 ~ 16
       UINT16 _byte3 :8; //bit 31 ~ 24
    }ByteCode;
} ULONG;

typedef union
{
    INT32 LongCode;
    struct
     {
         UINT16 _word0 :16; //LSB word code
        INT16 _word1 :16; //MSB word code
     }WordCode;
    struct
     {
          UINT16 _byte0 :8; //bit 7 ~ 0
          UINT16 _byte1 :8; //bit 15 ~ 8
         UINT16 _byte2 :8; //bit 23 ~ 16
         INT16 _byte3 :8; //bit 31 ~ 24
     }ByteCode;
}SLONG;

#define BYTE0 (ByteCode._byte0)
#define BYTE1 (ByteCode._byte1)
#define BYTE2 (ByteCode._byte2)
#define BYTE3 (ByteCode._byte3)
#define WORD0 (WordCode._word0)
#define WORD1 (WordCode._word1)
#define ByteCode(x) ((x._byte1<<8)|x._byte0)
#define WordCode(x) ((x._byte3<<24)|(x._byte2<<16)|(x._byte1<<8)|x._byte0)
#define Byte0(x) (x._byte0)
#define Byte1(x) (x._byte1)
#define Byte2(x) (x._byte2)
#define Byte3(x) (x._byte3)

以上的联合体定义与 define 有联系吗,有点不明白
dirtwillfly 发表于 2013-1-12 12:24 | 显示全部楼层
有啊,你看#define BYTE0 (ByteCode._byte0),括号里面的代码都是上面联合体定义的。
amwrdfe 发表于 2013-1-12 12:31 | 显示全部楼层
要看具体应用,对应哪个联合.
总之#define的作用就是替换.
 楼主| 醉心369 发表于 2013-1-12 13:25 | 显示全部楼层
amwrdfe 发表于 2013-1-12 12:31
要看具体应用,对应哪个联合.
总之#define的作用就是替换.

这个怎么对应啊
amwrdfe 发表于 2013-1-12 13:32 | 显示全部楼层
UWORD.BYTE0 = UWORD.ByteCode._byte0 ;

typedef union
{
    UINT16 WordCode;
    struct
    {
        UINT16 _byte0 : 8; //LSB byte code
        UInt16 _byte1 : 8; //MSB byte code
     } ByteCode;
}UWORD;

ByteCode结构成员的类型应该为u8才说得通
xupengtao7 发表于 2013-1-12 13:52 | 显示全部楼层
 楼主| 醉心369 发表于 2013-1-12 13:52 | 显示全部楼层
本帖最后由 醉心369 于 2013-1-12 16:05 编辑
amwrdfe 发表于 2013-1-12 13:32
UWORD.BYTE0 = UWORD.ByteCode._byte0 ;

typedef union

是不是结构体里面的_byte都是INT8 或UINT8才对呢?
我是说把下面的define与联合体对应起来?
ayb_ice 发表于 2013-1-12 14:06 | 显示全部楼层
定义都定义错了

这就是大小端+联合的问题
 楼主| 醉心369 发表于 2013-1-12 14:56 | 显示全部楼层
ayb_ice 发表于 2013-1-12 14:06
定义都定义错了

这就是大小端+联合的问题

怎么定义错了,望指出,这是我从一本书上看到的
ayb_ice 发表于 2013-1-12 15:52 | 显示全部楼层
醉心369 发表于 2013-1-12 14:56
怎么定义错了,望指出,这是我从一本书上看到的

不好意思,看错了

这个是用位域做的,位域有些不可移植性的,有些编译器是从前开始分配的,有些是从后开始分配的,其实不好
 楼主| 醉心369 发表于 2013-1-12 16:06 | 显示全部楼层
ayb_ice 发表于 2013-1-12 15:52
不好意思,看错了

这个是用位域做的,位域有些不可移植性的,有些编译器是从前开始分配的,有些是从后开始分 ...

结构体里面的_byte能用INT8 或UINT8类型吗
 楼主| 醉心369 发表于 2013-1-26 17:02 | 显示全部楼层
怎么没人回答啊,自己顶一下
amwrdfe 发表于 2013-1-27 11:57 | 显示全部楼层
呵呵,10#已经说得很清楚了,是用位域实现的.
yesiqi 发表于 2013-1-27 20:13 | 显示全部楼层
这个是哪本书上写的,我很想膜拜一下……
 楼主| 醉心369 发表于 2013-1-27 21:28 | 显示全部楼层
这是《删繁就简——单片机从入门到精通》
您需要登录后才可以回帖 登录 | 注册

本版积分规则

60

主题

283

帖子

2

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