public class Demo02 {
public static void main(String[] args) {
int i1 = 128;
int i2 = 129;
System.out.println(Integer.toBinaryString(i1));
System.out.println(Integer.toBinaryString(i2));
System.out.println(Integer.toBinaryString(i2&i1));
System.out.println(i1&i2);
}
}
输出结果:
10000000
10000001
10000000
128
------------------------------------------------------------------------------------
public class Demo02 {
public static void main(String[] args) {
int i1 = 128;
int i2 = 129;
System.out.println(Integer.toBinaryString(i1));
System.out.println(Integer.toBinaryString(i2));
System.out.println(Integer.toBinaryString(i2|i1));
System.out.println(i1|i2);
}
}
输出结果:
10000000
10000001
10000001
129
---------------------------------------------------------------------------
public class Demo02 {
public static void main(String[] args) {
int i1 = 128;
int i2 = 129;
System.out.println(Integer.toBinaryString(i1));
System.out.println(Integer.toBinaryString(i2));
System.out.println(Integer.toBinaryString(i2^i1));
System.out.println(i1^i2);
}
}
输出结果:
10000000
10000001
00000001(应该是1,但是为了方便理解)
1
---------------------------------------------------------------------------------
public class Demo02 {
public static void main(String[] args) {
int i1 = 128;
int i2 = 129;
System.out.println(Integer.toBinaryString(i1));
System.out.println(Integer.toBinaryString(i2));
System.out.println(Integer.toBinaryString(~i1));
System.out.println(~i1);
}
}
输出结果:
10000000
10000001
11111111111111111111111101111111
-129
------------------------------------------------------------------------------------
上述结果可以得出结论:
&(与):两个操作数中位都为1,结果才为1,否则结果为0;
|(或):两个操作数有一个为1时,结果就为1,否则结果为0;
^(异或):两个操作数不同时,结果为1,否则结果为0;
~(非):如果位为0,结果为1,反之,结果为0;