首页 > 应用开发 > Java > Java中"与"、"或"、"非"、"异或"
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;
int b = 9;
//8转换为二进制为1000
//9转换为二进制为1001
// 异或规则相同为0不同为1
// 1000
// | 1001
//--------------
// 0001
System.out.println(a^b);
//输出为1
}
}
int temp = {1,2,3,4,5,4,3,2,1};
public class Demo {
public static void main(String[] args) {
int[] temp = {1,2,5,2,1,4,4,15,5};
int a = 0;
for(int i=0;i<temp.length;i++) {
a = a ^ temp[i];
}
System.out.println(a);
}
}
C:\Users\PiLiPaLa\Desktop\Code\Eight>java Demo 15
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69983372/viewspace-2731107/ ,如需转载,请注明出处,否则将追究法律责任。