What is the exact function of the "|" (vertical line) operator when separating arguments

16,725

Solution 1

| 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.

Solution 2

 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)

Solution 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) { ... }

Solution 4

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

Solution 5

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.

Share:
16,725
Guy Joel McLean
Author by

Guy Joel McLean

Updated on June 16, 2022

Comments

  • Guy Joel McLean
    Guy Joel McLean almost 2 years

    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