What does the vertical pipe ( | ) mean in C++?

后端 未结 3 1725
心在旅途
心在旅途 2020-12-01 11:09

I have this C++ code in one of my programming books:

WNDCLASSEX wndClass = { 0 };
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.style =  CS_HREDRAW | CS_VRE         


        
相关标签:
3条回答
  • 2020-12-01 11:35

    | is called bitwise OR operator.

    || is called logical OR operator.

    0 讨论(0)
  • 2020-12-01 11:36

    It's a bitwise OR operator. For instance,

    if( 1 | 2 == 3) {
        std::cout << "Woohoo!" << std::endl;
    }
    

    will print Woohoo!.

    0 讨论(0)
  • 2020-12-01 11:45

    Bitwise OR operator. It will set all bits true that are true in either of both values provided.

    For example CS_HREDRAW could be 1 and CS_VREDRAW could be 2. Then it's very simple to check if they are set by using the bitwise AND operator &:

    #define CS_HREDRAW 1
    #define CS_VREDRAW 2
    #define CS_ANOTHERSTYLE 4
    
    unsigned int style = CS_HREDRAW | CS_VREDRAW;
    if(style & CS_HREDRAW){
        /* CS_HREDRAW set */
    }
    
    if(style & CS_VREDRAW){
        /* CS_VREDRAW set */
    }
    
    if(style & CS_ANOTHERSTYLE){
        /* CS_ANOTHERSTYLE set */
    }
    

    See also:

    • Wikipedia: Bitwise operation (Section OR)
    • Wikipedia: Mask (computing) Section( Common bitmask functions)
    0 讨论(0)
提交回复
热议问题