本帖最后由 醉心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 有联系吗,有点不明白 |