18 Aug Java Program implementing XOR on a set of Booleans
For implementing XOR on a set of Booleans, we have to use the XOR operator. Under a nested loop, perform the Bitwise XOR operation and display the result. Following are our set of Booleans i.e. Boolean array:
boolean[] arr = { false, true, false, false, false };
Following is the symbol of Bitwise XOR operator:
a ^ b
Example 1
Following is an example to implement XOR on a set of Booleans:
public class Example {
public static void main(String[] args) {
boolean[] bArray = { false, true, false, false };
System.out.println("Boolean Array...");
for (boolean b: bArray) {
System.out.println(b);
}
System.out.println("XOR of Boolean array...");
for (boolean b1 : bArray) {
for (boolean b2: bArray) {
boolean output = b1 ^ b2;
System.out.println(output);
}
}
}
}
Output
Boolean Array... false true false false XOR of Boolean array... false true false false true false true true false true false false false true false false
Example 2
public class Example {
public static void main(String[] args) {
boolean[] bArray = { false, false };
System.out.println("Boolean Array...");
for (boolean b: bArray) {
System.out.println(b);
}
System.out.println("XOR of Boolean array...");
for (boolean b1 : bArray) {
for (boolean b2: bArray) {
boolean output = b1 ^ b2;
System.out.println(output);
}
}
}
}
Output
Boolean Array... false false XOR of Boolean array... false false false false
No Comments