Click here to Skip to main content
14,239,386 members
Rate this:
Please Sign up or sign in to vote.
See more:
In the Win32 API(as well as in other API's), When passing arguments to some functions, I have seen and used the OR (|) to combine multiple parameters as one.

An Example:
CreateWindow(...,
	     WS_VISIBLE | WS_CHILD | WS_DISABLED
             ,...);


In the above function call it combines WS_VISIBLE, WS_CHILD, WS_DISABLED into one value. Calling a function like that is fine.

Problem:
How does the CreateWindow() function catch those flags separately ? (I would like to use this method in my own projects)
Posted
Updated 28-May-13 20:10pm
v3

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

This is done with the bit masks. Look at the values of WS_VISIBLE, WS_CHILD, etc.: http://msdn.microsoft.com/en-us/library/windows/desktop/ms632600%28v=vs.85%29.aspx[^].

As you can see, each of them is the order of 2. That is, each of them takes exactly one bit in the word. And you can always check a bit in one statement. Consider:

BOOL isVisible = (myWindowStyle & WS_VISIBLE) > 0;
BOOL isChild = (myWindowStyle & WS_CHILD) > 0;
// ...


Bit-masking techniques could be much more complex than that, yet simple enough and very effective.

See also: http://www.cprogramming.com/tutorial/bitwise_operators.html[^].

—SA
   
Comments
Kucera Jan 29-May-13 3:07am
   
Actually it would be more appropriate to test it for a non-zeor value, like this:

BOOL isVisible = ((myWindowStyle & WS_VISIBLE) != 0);
   
Why more appropriate? I prefer >, clear and correct. But this != is correct, too.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100