What does this sign exactly mean? |=

前端 未结 5 596
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-13 05:18

|=

I\'m curious to learn about this operator, I\'ve seen this notation used while setting flags in Java.

for example:

notification.flags |=         


        
5条回答
  •  猫巷女王i
    2021-01-13 05:48

    It is called bitwise or operator. For example,

    5     = 0000 0101
    2     = 0000 0010
    5 | 2 = 0000 0111
          = 14
    

    So, this concept is used when a same option can use multiple values.

    As an example, consider a variable flags equal to one.

    int flags = 1;
    

    Now, if a flag value of 4 is added to it with bitwise or,

    flags |= 4;  // 0
    

    You can determine whether 4 was applied on flags with the bitwise and.

    if (flags & 4 == 4)  // returns true
    

    If that flag has been applied on the flags, bitwise and returns flag. In this way we can use bitwise and & bitwise or.

    Hope this helps.

提交回复
热议问题