Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
3.00/5 (1 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:
C++
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

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:

C++
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
 
Share this answer
 
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);
Sergey Alexandrovich Kryukov 29-May-13 3:15am    
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900