tags:

views:

321

answers:

7

I usually see this when looking at Win32 gui code. My assumption is that it is a standard bitwise or, but I also occasionaly see it in C#, and it seems like there would be a better (well higher level) way to do the same thing there. Anyway, here's an example:

MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);

Thanks,

Seamus

+4  A: 

It's the bitwise or operator like other places. Basically, this technique is used when you want to set some attributes that are not mutually exclusive.

The function can easily check them with some code like this:

if (arg & MB_ICONEXCLAMATION) { // Show an exclamation icon...

}

// ...

if (arg & MB_OK) { // Show an OK button

}
Mehrdad Afshari
So i guess that this would be used to combine flags such as 0001 and 0010 into 0011 making for a hackish(?) variable argument function call
Seamus
It's a common technique in Windows API. Yeah, that's right.
Mehrdad Afshari
Its a common technique in a lot of places, including *nix system calls. Take a look at the modes and flags of open(), for example. Using a long parameter to pass a collection of up to 32 flags to control details of an API is an efficient use of the stack, not to mention making the thunk from user space to system space needed for a system call easier to manage than many of the more elaborate ways you might imagine using.
RBerteig
Yeah, I said it since the OP was about WinAPI. It's used in lots of places, even in .NET, there are use cases for this technique in [Flags] enums.
Mehrdad Afshari
A: 

It does a bitwise or.

int x = 5 | 3; // x == 7 now

Martin York
No, the binary for 5 is 101 and the binary for 3 is 011, so 5 | 3 is binary 111 or 7.
David Thornley
I think you need to check your math. 5 | 3 is 7. (101)_2 | (011)_2 = (111)_2
Welbog
Ahh binary head hurts....
Martin York
+1  A: 

It's a bitwise OR.

jmucchiello
+16  A: 

The | is a bitwise OR. MB_OK and MB_ICONEXCLAMATION are defined constants which are a power of 2 (such as 32 or 128), so that the bitwise OR can combine them (128 | 32 would be 160, which has two bits set). This is normal when the bits are used as flags.

David Thornley
Also note that it's not specific to function calls, it behaves the same way everywhere.
Bastien Léonard
+10  A: 

Its for bitmasking. Lets use this incredibly trivally example. You have a binary value for colors, with the following values.

100 = BLUE

010 = RED

001 = GREEN

When you say SomeFunction ( BLUE | RED | GREEN ); you are infact passing the value 111, which can then be decoded to mean BLUE and RED and GREEN.

Google Bitwise operators for more details.

Serapth
Thanks a lot, I guess if I had given it some more thought, I would have realized that this was all its doing. But it never hurts to ask I suppose, especially on stack overflow. Thanks!
Seamus
No problems. Better to ask and learn than stay quite and learn nothing. It is one of those things all scattered throughout Win32 and MFC, but until you stop and think about how it works, it might as well be magic. :)
Serapth
unwind
+3  A: 

Think of MB_ICONEXCLAMATION and MB_OK as "options" that aren't anything fancier than ints. What you care about is the bit representation of those ints.

Say:

//MessageBox.cs or whatever
public static int MB_ICONEXCLAMATION = 0x1 // 0001 in binary
public static int MB_OK = 0x2 // 0010 in binary

When you OR them together, you get 0011 in binary. So you are requesting both options for the MessageBox using just one argument instead of having to have more arguments, one for each option you want to specify.

Anzurio
A: 

Read the K&R. It's incredible to post such a question...