BYTE,WORD,DWORD的大小及一些特殊的"高低位宏"

一、定义

在VC6.0的Microsoft Visual Studio/VC98/Include/windef.h 里,定义了BYTE,WORD,DWORD

typedef unsigned long       DWORD;

typedef unsigned char       BYTE;

typedef unsigned short      WORD;

在Visual C++ 6.0中,char型长度为1字节,short型长度为2字节,int和long型长度都为4字节,因此可以认为BYTE与WORD,DWORD定义的变量分别获得了1字节,2字节,4字节内存,正与BYTE与WORD,DWORD的字面意义相符。

即:

BYTE=unsigned char(完全等同):       8位

WORD=unsigned short(完全等同):  16位

DWORD=unsigned long(完全等同): 32位

 

 

 

下面是一些平时用得比较少的宏:

二、Window 宏

1.LOBYTE( 从给定16位值中提取低位字节)

BYTE LOBYTE(

  WORD wValue  // value from which low-order byte is retrieved

);

#define LOBYTE(w)   ((BYTE) (w))

如:WORD w=-0x1234;//w在内存中以补码形式存储:0xedcc,即0xcc(低地址) 0xed(高地址)

printf("w的低位字节:%x/n",LOBYTE(w));//输 出:w的低位字节:cc

 

2.HIBYTE( 从给定16位值中提取高位字节)

BYTE HIBYTE(

  WORD wValue  // value from which high-order byte is retrieved

);

#define HIBYTE(w)   ((BYTE) (((WORD) (w) >> 8) & 0xFF))

如:printf("w的高位字节:%x/n",HIBYTE(w));// 输出:w的高位字节:ed

 

3.LOWORD(从给定32位值中提取低位word)

WORD LOWORD(

  DWORD dwValue  // value from which low-order word is retrieved

);

#define LOWORD(l)   ((WORD) (l))

如: DWORD l = 0x12345678;//l在内存中存储为:0x78,0x56,0x34,0x12

printf("l的低位字:%x/n",LOWORD(l));// 输出:l的低位字:5678

 

4.HIWORD(从给定32位值中提取高位word)

WORD HIWORD(

  DWORD dwValue  // value from which high-order word is retrieved

);

#define HIWORD(l)   ((WORD) (((DWORD) (l) >> 16) & 0xFFFF))

如:printf("l的高位字:%x/n",HIWORD(l));//输出:l的高位字:1234

5.MAKEWORD(将两个给定的无符号字符值连接成为一个16位整数)

WORD MAKEWORD(

  BYTE bLow,  // low-order byte of short value

  BYTE bHigh  // high-order byte of short value

);

#define MAKEWORD(a, b) /

((WORD) (((BYTE) (a)) | ((WORD) ((BYTE) (b))) << 8))

如: BYTE bLow=0x34,bHigh=0x12;

printf("MAKEWORD(%x,%x)=%x/n",bLow,bHigh,MAKEWORD(bLow,bHigh));

输出结果为: MAKEWORD(34,12)=1234

 

6.MAKELONG(将两个给定的16位值连接成为一个32位整数)

DWORD MAKELONG(

  WORD wLow,  // low-order word of long value

  WORD wHigh  // high-order word of long value

);

#define MAKELONG(a, b) /

    ((LONG) (((WORD) (a)) | ((DWORD) ((WORD) (b))) << 16))

 

如:WORD vLow=0x5678,vHigh=0x1234;

printf("MAKELONG(%x,%x)=%x/n",vLow,vHigh,MAKELONG(vLow,vHigh));

输出结果:MAKELONG(5678,1234)=12345678

你可能感兴趣的:(c,Microsoft,存储,byte)