问题描述
我在一个教学法中遇到这个宏 MAKEWORD(2,2)
。我在MSDN中读到它通过连接指定的值创建一个WORD值。
问题是,不是一个WORD的东西像一个无符号整数为什么我需要做一个奇怪的过程,如使用 MAKEWORD()
?
宏需要两个字节作为参数:
WORD MAKEWORD BYTE bLow,
BYTE bHigh
);
在 Windef.h
中定义为:
#define MAKEWORD(a,b)((WORD)((BYTE)(a))| )((BYTE)(b)))
来自两个1字节字的16位字(并且看起来很不方便)
具有1字节(WORD)的数字2的二进制表示是:
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
如果我们把 MAKEWORD(2,2)
中的两个字节连接起来, :
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
这是512 + 2 = 514:,以生成 WSAStartup $ c $所期望的版本字词c>。
I have come across this macro MAKEWORD(2,2)
in a piece of instructional code. I read in MSDN that it "Creates a WORD value by concatenating the specified values."
The question is, isn't a WORD something like an unsigned integer why would I ever need to do such a strange procedure such as using MAKEWORD()
?
The macro expects two bytes as its parameters:
WORD MAKEWORD(
BYTE bLow,
BYTE bHigh
);
Its defined in Windef.h
as :
#define MAKEWORD(a,b) ((WORD)(((BYTE)(a))|(((WORD)((BYTE)(b)))<<8)))
It basically builds a 16 bits words from two 1 bytes word (and doesn't look very portable)
The binary representation of the number 2 with 1 byte (a WORD) is :| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
If we take the concatenate two of those bytes as in MAKEWORD(2,2)
, we get:
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
Which is 512 + 2 = 514 : live demo.
The only real life example of this particular macro is in the Initialization of Winsock, to generate the versioning word expected by WSAStartup
.
这篇关于MAKEWORD是什么用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!