0

I couldn't be very accurate with the title of my question, because I don't know how this thing is really called.

I keep seeing how the Win32 API stores more then 1 value in a variable.

For example, WPARAM and LPARAM. it's a long number that is "divided" to two: high word and low word. There are many more examples when it takes 32bit value, divides it to 2 and sets it to two 16 bit values.

How can I set a variable myself this way? (create a variable, and divide the bits to how many I need, and set each part)

P.S. I'm asking mainly for educational purposes, so even if you wouldn't recommend me to do this myself, I would still appreciate an answer. Thanks.

MasterMastic
  • 20,711
  • 12
  • 68
  • 90
  • Have a look at [this SO question](http://stackoverflow.com/questions/2515261/what-are-the-definitions-for-lparam-and-wparam). Once you sort out your confusion, you can re-formulate your question :) – Alexander Pavlov Jun 04 '12 at 08:43
  • @AlexanderPavlov Thank you for the constructive comment. I'll have a look. – MasterMastic Jun 04 '12 at 08:44
  • 2
    Note that it's easier to introduce bugs this way, especially if you're not familiar with bit manipulation, and modern PCs have enough ram to not really need this feature any more. Except when they run Eclipse :) IMO clearer code wins most of the time, unless you've run a profiler and *that* is your performance bottleneck. – Torp Jun 04 '12 at 08:49
  • @Torp Granted, and I couldn't agree more. thank you. – MasterMastic Jun 04 '12 at 08:53
  • Windows doesn't use such methods in newer API's, actually. The `WPARAM` and `LPARAM` types you mention date back to the eighties. – MSalters Jun 04 '12 at 09:43
  • @MSalters Then to what we refer when we use the macro `LOWORD` and `HIWORD`? – MasterMastic Jun 04 '12 at 11:07
  • @Ken: Those macro's are also associated with older API's. – MSalters Jun 04 '12 at 12:30
  • @MSalters True, but they're still being used today. nobody can deny that. – MasterMastic Jun 04 '12 at 13:12

3 Answers3

1

You could do this:

WPARAM param = (WPARAM)(hiByte << 8 | lowByte);

That sets the 16 bits of the variable param.

And the reverse operation is this:

//BYTE  is defined in WinDef.h
BYTE lowByte = (BYTE)(param && 0xFF);
BYTE hiByte =  (BYTE)((param >> 8) && 0xFF);

Or you could use the macros LOBYTE and HIBYTE defined in WinDef.h as:

BYTE lowByte = LOBYTE(param);
BYTE hiByte =  HIBYTE(param);
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • I may ask for too much, but could you throw a reference here so I could learn what this code really is? I tried to read bitwise operators guides and tutorials, but none of them have taught a practical use. – MasterMastic Jun 04 '12 at 08:50
1

There are (at least) two ways to achieve this:

  • using the bit shift / mask operators <<, >>, & and |, something like this (just an example, not tested):

    int loWord = ...;
    int hiWord = ...;
    LPARAM param = (hiWord << 16) | loWord;
    ...
    loWord = param & 0xFFFF;
    hiWord = param >> 16;
    
  • using a union type (although this latter one is subject to memory alignment and endianness issues, so it may not be a portable solution); see an example here.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
1

Its usually called a bitmask, and one way to define them could be as follows:

struct Param
{
   unsigned wparam   : 16;     //16 bits
   unsigned lparam   : 16;     //16 bits
};

Generally its not recommended to define them this way because they are not portable. The other answers here show a more portable way to do it.

Brady
  • 10,207
  • 2
  • 20
  • 59