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:
1 2 3 |
boolean[] arr = { false, true, false, false, false }; |
Following is the symbol of Bitwise XOR operator:
1 2 3 |
a ^ b |
Example 1
Following is an example to implement XOR on a set of Booleans:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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
1 2 3 4 5 6 7 8 9 10 |
Boolean Array... false false XOR of Boolean array... false false false false |
No Comments