A sign in C++ I've never seen before: |=

后端 未结 5 1202
盖世英雄少女心
盖世英雄少女心 2021-01-25 03:31

I have some code here:

case MONITORTYPE_WUXGA_SXGA_WXGA:
    bResult |= (var == enum1);
    bResult |= (var == enum2);

Now i know what its doin

相关标签:
5条回答
  • 2021-01-25 04:05

    | is the bitwise or Operator. a |= b is equal to a = a | b.

    More on bitwise operations: http://en.m.wikipedia.org/wiki/Bitwise_operation

    0 讨论(0)
  • 2021-01-25 04:11

    It's equivalent to:

    bResult = bResult | (var == enum1);
    

    Just like a += b means a = a + b, a |= b means a = a | b.

    0 讨论(0)
  • 2021-01-25 04:18
    a |= b
    

    is the same as

    a = a | b
    

    which is a bitwise OR operation.

    0 讨论(0)
  • 2021-01-25 04:19

    It's a bitwise OR. It means the same as bResult = bResult | (value). In this case, it is setting bResult to true if var is either enum1 or enum2.

    0 讨论(0)
  • 2021-01-25 04:20

    For most binary operators in C++ (except comparison operators, relational operators and boolean operators), there exists a corresponding compound assignment operator, ♢=.

    That is, |= is simply the compound assignment operator for | which is bitwise or. Its use is completely equivalent to +=, *= etc. So

    a |= b;
    // is equivalent to
    a = a | b;
    
    0 讨论(0)
提交回复
热议问题