转载:http://blog.csdn.net/ziwen00/article/details/40656897
代码:
public class MainMethod {
public static void main(String[] args) {
boolean flag = true;
flag &= true;
System.out.println("true\t&=\ttrue\t==>\t" + flag);
flag = true;
flag &= false;
System.out.println("true\t&=\tfalse\t==>\t" + flag);
flag = false;
flag &= true;
System.out.println("false\t&=\ttrue\t==>\t" + flag);
flag = false;
flag &= false;
System.out.println("false\t&=\tfalse\t==>\t" + flag+"\n");
flag = true;
flag |= true;
System.out.println("true\t|=\ttrue\t==>\t" + flag);
flag = true;
flag |= false;
System.out.println("true\t|=\tfalse\t==>\t" + flag);
flag = false;
flag |= true;
System.out.println("false\t|=\ttrue\t==>\t" + flag);
flag = false;
flag |= false;
System.out.println("false\t|=\tfalse\t==>\t" + flag+"\n");
System.out.println("^= 相同为真,不同为假");
flag = true;
flag ^= true;
System.out.println("true\t^=\ttrue\t==>\t" + flag);
flag = true;
flag ^= false;
System.out.println("true\t^=\tfalse\t==>\t" + flag);
flag = false;
flag ^= true;
System.out.println("false\t^=\ttrue\t==>\t" + flag);
flag = false;
flag ^= false;
System.out.println("false\t^=\tfalse\t==>\t" + flag);
}
}
public static void main(String[] args) {
boolean flag = true;
flag &= true;
System.out.println("true\t&=\ttrue\t==>\t" + flag);
flag = true;
flag &= false;
System.out.println("true\t&=\tfalse\t==>\t" + flag);
flag = false;
flag &= true;
System.out.println("false\t&=\ttrue\t==>\t" + flag);
flag = false;
flag &= false;
System.out.println("false\t&=\tfalse\t==>\t" + flag+"\n");
flag = true;
flag |= true;
System.out.println("true\t|=\ttrue\t==>\t" + flag);
flag = true;
flag |= false;
System.out.println("true\t|=\tfalse\t==>\t" + flag);
flag = false;
flag |= true;
System.out.println("false\t|=\ttrue\t==>\t" + flag);
flag = false;
flag |= false;
System.out.println("false\t|=\tfalse\t==>\t" + flag+"\n");
System.out.println("^= 相同为真,不同为假");
flag = true;
flag ^= true;
System.out.println("true\t^=\ttrue\t==>\t" + flag);
flag = true;
flag ^= false;
System.out.println("true\t^=\tfalse\t==>\t" + flag);
flag = false;
flag ^= true;
System.out.println("false\t^=\ttrue\t==>\t" + flag);
flag = false;
flag ^= false;
System.out.println("false\t^=\tfalse\t==>\t" + flag);
}
}
执行结果:
true &= true ==> true
true &= false ==> false
false &= true ==> false
false &= false ==> false
true |= true ==> true
true |= false ==> true
false |= true ==> true
false |= false ==> false
^= 相同为真,不同为假
true ^= true ==> false
true ^= false ==> true
false ^= true ==> true
false ^= false ==> false