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

Categories

0 votes
716 views
in Technique[技术] by (71.8m points)

c++ - What is MAKEWORD used for?

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

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...