& 按位"与"操作符
public class Demo {
public static void main(String[] args) {
// &操作符
int a = 16;
//16转换为二进制为10000
int b = 17;
//17转换为二进制为10001
// 两个操作数都为1时才为1
// 10000
// & 10001
//--------------
// 10000
System.out.println(a&b);
//输出为16
}
}
| 按位"或"操作符
public class Demo {
public static void main(String[] args) {
// |操作符 int a = 16;
//16转换为二进制为10000
int b = 17;
//17转换为二进制为10001
// 两个操作数只要有一个1就为1
// 10000
// | 10001
//--------------
// 10001
System.out.println(a|b);
//输出为17
}
}
~ "取反"操作符
public class Demo {
public static void main(String[] args) {
// ~操作符
int a = 8;
//8转换为二进制为1000
// 补符号位为
// 01000
// 进行取反运算为
// 10111 (补码)
// 转源码除符号位取反+1
// 11001
System.out.println(~a);
//输出为-9
}
}
^ "异或"操作符
public class Demo {
public static void main(String[] args) {
// ^操作符
int a = 8;
int b = 9;
//8转换为二进制为1000
//9转换为二进制为1001
// 异或规则相同为0不同为1
// 1000
// | 1001
//--------------
// 0001
System.out.println(a^b);
//输出为1
}
}