今天,在看一段代码的时候突然发现其中有这样一个运算符: |=
由于在Google和baidu上是无法搜索的,就到书上查,其中提到了这个运算符,但是并没有详细说明其用法!
本人大胆猜其用法为“按位或,赋值”。写了一段代码验证了一下。
public class temp {
public static void main(String[] args) {
int i1 = 4;
int j1 = 5;
int i2 = 3;
int j2 = 6;
i1 |= j1;
System.out.println("按位或结果: "+i1);
i2 &= j2;
System.out.println("按位与结果: "+i2);
}
}
输出结果证明,|= 运算规则是:
a |= b;
相当于:
a = a | b;
&= 同理。