这个标志究竟意味着什么? | =

时间:2013-11-17 13:29:42

标签: java android bit-manipulation flags

| =

我很想知道这个算子, 我在Java中设置标志时看到过这种符号。

例如:

notification.flags |= Notification.FLAG_AUTO_CANCEL;
  1. 它是否执行某种位操作?

  2. 这个标记到底是做什么的?

  3. 还有其他众所周知的标志吗?

5 个答案:

答案 0 :(得分:8)

相当于

notification.flags = notification.flags | Notification.FLAG_AUTO_CANCEL;

其中|是按位OR运算符,其中两个变量是逐位的。

它本身是众所周知的。还有+=-=*=/=%=&=^=

答案 1 :(得分:3)

此语法也可用于C / C ++和其他语言。它是一个按位OR,与:

相同
notification.flags = notification.flags | Notification.FLAG_AUTO_CANCEL;

和其他运算符类似,例如加法,减法等。例如:

i += 5;

与:

相同
i = i + 5;

答案 2 :(得分:3)

  

(1)它是否执行某种位操作?

如果你使用|和数字操作数,那么它将是bitwise或者,如果你在布尔操作数上使用它,它将是逻辑(非短路)OR。实施例

逻辑OR:我们可以说1代表true0 false

+---+---+-------+
| p | q | (p|q) |
+---+---+-------+
| 0 | 0 |   0   |
| 0 | 1 |   1   |
| 1 | 0 |   1   |
| 1 | 1 |   1   |
+---+---+-------+

按位OR将执行与布尔类似的操作,但将使用相应的位

decimal   binary
6       = 00110
3       = 00011
OR -------------
          00111
  

(2)这个标记到底是做什么的?

x |= yx = x | y相同,因此会计算x | y并将其存储在x

  

(3)还有其他众所周知的标志吗?

是的,每个算术,按位或位移运算符都可以这样使用:+= -= *= /= %= &= { {1}} ^= |= <<= >>=


以下是有关

>>>=用法的一些其他信息
|=

让我们说我们有五个属性。

notification.flags |= Notification.FLAG_AUTO_CANCEL;

我们可以使用最后五位的数字来表示我们有(1)或者没有(0)某些属性的情况。

Property1 
Property2
Property3
Property4
Property5

现在,假设我们只想使用属性1,2和4.要做到这一点,我们必须将索引为0,1和3的位设置为值1,如

...00xxxxx
     │││││
     ││││└─ flag for Property1
     │││└── flag for Property2
     ││└─── flag for Property3 
     │└──── flag for Property4
     └───── flag for Property5

换句话说,我们必须产生13号(= ** 1 *** 8 + ** 1 *** 4 + ** 0 *** 2 + ** 1 *** 1)。我们可以使用...0001101 │││││ ││││└─ (1) has Property1 │││└── (0) no Property2 ││└─── (1) has Property3 │└──── (1) has Property4 └───── (0) no Property5 或按位运算符|执行此操作,因为

8|4|1

但为了避免magic numbers,我们可以创建代表我们在bit world中的属性的常量。所以我们可以创建

   8 = ...001000
   4 = ...000100
   1 = ...000001
OR -------------
  13 = ...001101

稍后再使用

public class MyProperties {

    //...
    public static final int PROPERTY_1 = 0b0000_0001; // = 1
    public static final int PROPERTY_2 = 0b0000_0010; // = 2
    public static final int PROPERTY_3 = 0b0000_0100; // = 4
    public static final int PROPERTY_4 = 0b0000_1000; // = 8
    public static final int PROPERTY_5 = 0b0001_0000; // = 16
    //...
    //rest of code: methods, constructors, other fields
}

int context = Properties.PROPERTY_1|Properties.PROPERTY_2|Properties.PROPERTY_4 更具可读性。


现在,如果我们想要更改我们的上下文,让我们说添加int context = 8|4|1我们可以使用

PROPERTY_3

或基于复合赋值运算符的较短版本

context = context | Properties.PROPERTY_3;

将进行此计算

context |= Properties.PROPERTY_3;

(将 context = 000...00001101 PROPERTY_3 = 000...00000010 OR ------------------------ 000...00001111 添加到PROPERTY_3并使用按位OR context之间的主要区别在于|已将context设置为{{ 1}}然后OR不会影响它。)


现在,如果您看一下here使用单个位作为属性的标志的想法,则在PROPERTY_3类中使用,其中1常量具有值Notification({{ 1}}十六进制,FLAG_AUTO_CANCEL二进制)。

答案 3 :(得分:2)

  

它是否执行某种位操作?

是。它是右手边的操作数“或”左边的操作数。

  

这个标记到底是做什么的?

这是一个与OR结合的作业。

  

还有其他众所周知的迹象吗?

有很多:+=-=*=/=%=&=等等。总的来说,它们被称为复合赋值运算符They are described in section 15.26.2 of the Java Language Specification.

答案 4 :(得分:-1)

它被称为按位或运算符。例如,

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

因此,当同一选项可以使用多个值时,会使用此概念。

例如,考虑一个等于一的变量标志。

int flags = 1;

现在,如果使用按位或

向其添加标志值4
flags |= 4;  // 0

您可以确定是否在按位和。

的标志上应用了4
if (flags & 4 == 4)  // returns true

如果该标志已应用于标志,则按位并返回标志。通过这种方式,我们可以使用按位和&amp;按位或。

希望这有帮助。