What does the |= operator do in Java?

前端 未结 6 738
暗喜
暗喜 2020-12-06 10:15

While reading the Android guide to Notifications, I stumbled across this:

Adding vibration

You can alert the user with th

6条回答
  •  甜味超标
    2020-12-06 10:39

    |= is a bitwise-OR-assignment operator. It takes the current value of the LHS, bitwise-ors the RHS, and assigns the value back to the LHS (in a similar fashion to += does with addition).

    For example:

    foo = 32;   // 32 =      0b00100000
    bar = 9;    //  9 =      0b00001001
    baz = 10;   // 10 =      0b00001010
    foo |= bar; // 32 | 9  = 0b00101001 = 41
                // now foo = 41
    foo |= baz; // 41 | 10 = 0b00101011 = 43
                // now foo = 43
    

提交回复
热议问题