Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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()?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
706 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...