3

I've used the double vertical "||" as the boolean "or" operator. And seen that the "|" is the bitwise or.

However, since I've started working with c++/cli I've noticed it used to separate flags in functions with a single parameter that seem to accept multiple flags.

An example of this would be in msdn's example of the MessageBox() function.

int msgboxID = MessageBox(
        NULL,
        (LPCWSTR)L"Resource not available\nDo you want to try again?",
        (LPCWSTR)L"Account Details",
        MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2
    );

What exactly is the operation performed by "|" here?

What is the "|" symbol actually called? (Like the "^" is called a caret, rather than what I knew it as before i programmed, which was "upside down V") :D

The reason I ask is that I'm using the function setWindowPos(), which also accepts flags as parameters. The function declared like so:

BOOL WINAPI SetWindowPos(
  _In_      HWND hWnd,
  _In_opt_  HWND hWndInsertAfter,
  _In_      int X,
  _In_      int Y,
  _In_      int cx,
  _In_      int cy,
  _In_      UINT uFlags
);

And I wanted to know whether the flags can be combined in the same way as in MessageBox().

Thanks in advance,

Guy

Guy Joel McLean
  • 1,019
  • 4
  • 12
  • 34
  • This is logical or operator. – Tio Pepe Jun 28 '13 at 14:13
  • 3
    @TioPepe No it's not, it's the _bitwise_ or operator! – zennehoy Jun 28 '13 at 14:14
  • My guess is `MB_ICONWARNING`, `MB_CANCELTRYCONTINUE`, `MB_DEFBUTTON2` are macros defined somewhere in the code and `|` is the bitwise or operator applied on these. – Aman Deep Gautam Jun 28 '13 at 14:14
  • | is just the **bitwise OR** (and the bitwise or is the operation it performs). Yes flags for SetWindowPos can be combined like that (you know when you can do it because **MSDN says** "_This parameter can be a combination of the following values_"). Oh...anyway that code is not very c++/cli... ;) – Adriano Repetti Jun 28 '13 at 14:14
  • `|` is usualy called vertical bar or pipe. – jrok Jun 28 '13 at 14:17

5 Answers5

8

| is the bitwise or operator. It's used in the way you describe when multiple values can be combined to produce different effects. For example:

unsigned char MB_ICONWARNING = 1; //00000001
unsigned char MB_CANCELTRYCONTINUE = (1 << 1); //00000010
unsigned char MB_DEFBUTTON2 = (1 << 2); //00000100

Let's say you want a message box with that has all the properties represented by those values, you can specify that by bitwise or'ing them:

unsigned char combined = MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2; //00000111

The called function can then use them to determine the options you requested with the bitwise & operator like this

if(options & MB_ICONWARNING)
{
    //Do MB_ICONWARNING 
}

if(options & MB_CANCELTRYCONTINUE)
{
    //Do MB_CANCELTRYCONTINUE
}

//etc...

If you're interested, you can read more on Bit Fields.

anthonyvd
  • 7,329
  • 4
  • 30
  • 51
  • Okay thank I understand now. I simply assumed that it wouldnt be another "or", because if I say what I'm actually doing here in my head, I naturally verbalise it as "and". "The flags I'm going to set are MB_ICONWARNING and MB_CANCELTRYCONTINUE and MB_DEFBUTTON2". Just one of those things I found confusing. As mentioned in a comment on an above answer, apparently naive programmers use + instead. I'd guess this is because it seems more natural. Anyway thanks. – Guy Joel McLean Jun 28 '13 at 14:44
  • Yeah the thing is + would screw you up if your one of your bitfield values is more than a bit large and steps over another one. Glad I could help! – anthonyvd Jun 28 '13 at 14:49
5
 unsigned int flag = MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2

Here, | is still the bitwise or operator. The arguments it separates are macros of some bit patters. They are probably defined like this:

#define MB_ICONWARNING       1
#define MB_CANCELTRYCONTINUE 2
#define MB_DEFBUTTON2        4

So that inside the function MessageBox, options can be checked like this to check if the MB_ICONWARNING bit is on.

if (flag & MB_ICONWARNING)
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
3

As long as the flags can be uniquely identified by their bits (for example by being powers of two), then they can be combined using the bitwise or operator as you're trying to do.

Consider:

MB_ICONWARNING = 1;
MB_CANCELTRYCONTINUE = 2;

then

flags = MB_ICONWARNING | MB_CANCELTRYCONTINUE; // == 3

To check against flags, you can then use bitwise and:

if(flags & MB_ICONWARNING) { ... }
zennehoy
  • 6,405
  • 28
  • 55
  • 1
    `As long as the flags are defined to be one bit each` No they're not, they don't need to contain only one bit. – Lie Ryan Jun 28 '13 at 14:19
  • 1
    actually flags do not have to be a power of two, and they can contain more than 1 bit – Enigma Jun 28 '13 at 14:20
  • @LieRyan True, changed wording to reflect that. – zennehoy Jun 28 '13 at 14:20
  • Naive programmers use + instead of |, but that fails badly when flag masks are not simply one bit each. That's not the fault of the library; the masks are probably useful, like defining `READ` as `0x4`, `WRITE` as `0x2`, `EXECUTE` as `0x1`, and `ALL` as `READ | WRITE| EXECUTE`. Just don't accidentally calculate `ALL + EXECUTE`. – Eric Jablow Jun 28 '13 at 14:29
2

The symbol is called one of the following: vertical-bar, vbar, vertical line or vertical slash:

http://www.theasciicode.com.ar/ascii-printable-characters/vertical-bar-vbar-vertical-line-vertical-slash-ascii-code-124.html

It performs bitwise OR:

http://en.wikipedia.org/wiki/Bitwise_operation

Enigma
  • 1,699
  • 10
  • 14
1

If you will look here at the pattern which all those errors have (I mean their value representation) you will notice that there is a lot of sence bitwise ORing them. Every group of flags reserves a 4 bit block an acording to the specific flag the values of that block are modified.

The combinations are chosed that way so the bitwise OR could work as a + operator.

MB_ICONWARNING...............0x00000030L

MB_DEFBUTTON2................0x00000100L

MB_CANCELTRYCONTINUE.0x00000006L

Bitwise OR result...................0x00000136L

As the 3rd parameter of the MessageBox stands for

The contents and behavior of the dialog box.

The compiler can now easily check acording to the bitwise result what to display.

Alexandru Barbarosie
  • 2,952
  • 3
  • 24
  • 46