What does the |= operator do in Java?

前端 未结 6 711
暗喜
暗喜 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条回答
  •  萌比男神i
    2020-12-06 10:55

    a |= x is a = a | x, and | is "bitwise inclusive OR"

    Whenever such questions arise, check the official tutorial on operators.

    Each operator has an assignment form:

    += -= *= /= %= &= ^= |= <<= >>= >>>=

    Where a OP= x is translated to a = a OP x

    And about bitwise operations:

       0101 (decimal 5)
    OR 0011 (decimal 3)
     = 0111 (decimal 7)
    

    The bitwise OR may be used in situations where a set of bits are used as flags; the bits in a single binary numeral may each represent a distinct Boolean variable. Applying the bitwise OR operation to the numeral along with a bit pattern containing 1 in some positions will result in a new numeral with those bits set.

提交回复
热议问题