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


    一、定义

    VC6.0Microsoft 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

     

    from:http://blog.csdn.net/zhangyang0402/archive/2008/12/13/3509850.aspx

  • 相关阅读:
    前端备战21秋招之操作系统,线程/进程/死锁
    前端备战秋招之计算机网络,这一篇足矣
    VS Code项目中共享自定义的代码片段方案
    eslint插件开发教程
    2020前端春招经验分享,从面试小白到老油条的蜕变
    使用nodejs从控制台读入内容
    js实现展开多级数组
    js使用typeof与instanceof相结合编写一个判断常见变量类型的函数
    07-数据结构
    06-流程控制
  • 原文地址:https://www.cnblogs.com/dartagnan/p/2003608.html
Copyright © 2020-2023  润新知