private static Integer a = 128;
private static Integer b = 129;
public static void main(String[] args) {
a();
b();
c();
d();
}
/**
* 与运算
*/
public static void a(){
System.out.println("与运算");
System.out.println(Integer.toBinaryString(a)+"&"+Integer.toBinaryString(b));
System.out.println(Integer.toBinaryString(a&b));
System.out.println(a+"&"+b);
System.out.println(a&b);
}
/**
* 或运算
*/
public static void b(){
System.out.println("或运算");
System.out.println(Integer.toBinaryString(a)+"|"+Integer.toBinaryString(b));
System.out.println(Integer.toBinaryString(a|b));
System.out.println(a+"|"+b);
System.out.println(a|b);
}
/**
* 非运算
*/
public static void c(){
System.out.println("非运算");
System.out.println("~"+Integer.toBinaryString(a));
System.out.println(Integer.toBinaryString(+(~a)));
System.out.println("~"+a);
System.out.println(~a);
}
/**
* 异或运算
*/
public static void d(){
System.out.println("异或运算");
System.out.println(Integer.toBinaryString(a)+"^"+Integer.toBinaryString(b));
System.out.println(Integer.toBinaryString(a^b));
System.out.println(a+"^"+b);
System.out.println(a^b);
}
与运算
10000000&10000001
10000000
128&129
128
或运算
10000000|10000001
10000001
128|129
129
非运算
~10000000
11111111111111111111111101111111
~128
-129
异或运算
10000000^10000001
1
128^129
1
1.与运算符
与运算符用符号“&”表示,其使用规律如下:
两个操作数中位都为1,结果才为1,否则结果为0
2.或运算符
或运算符用符号“|”表示,其运算规律如下:
两个位只要有一个为1,那么结果就是1,否则就为0
3.异或运算符
异或运算符是用符号“^”表示的,其运算规律是:
两个操作数的位中,相同则结果为0,不同则结果为1
4.非运算符
非运算符用符号“~”表示,其运算规律如下:
如果位为0,结果是1,如果位为1,结果是0