In this tutorial, we will see about XOR operator in java.
XOR operator
or exclusive OR
takes two boolean operands and returns true if two boolean operands are different.
XOR operator can be used when both the boolean conditions can’t be true simultaneously.
Here is truth table for XOR operator
.
P | Q | P XOR Q |
---|---|---|
true | true | false |
true | false | true |
false | true | true |
false | false | false |
Let’s say you have Person
class and you want to filter persons who is either Male or Adult but not both
from list of persons.
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package org.arpit.java2blog; public class Person { String name; int age; String gender; public Person(String name, int age, String gender) { super(); this.name = name; this.age = age; this.gender = gender; }; // getters and setters public boolean isMale() { if(gender.equalsIgnoreCase("Male")) { return true; } return false; } public boolean isAdult() { if(age>=18) { return true; } return false; } public String toString() { return "Name:"+name+" Age:"+age+" Gender:"+gender; } } |
Create Main classed named PersonXORMain.java
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.List; public class PersonXORMain { public static void main(String[] args) { // Filter list with if person is either male or greater than 18 but not both List<Person> personList = getPersonList(); List<Person> filteredList = new ArrayList<>(); for(Person p :personList) { if((p.isMale() && !p.isAdult()) || (!p.isMale() && p.isAdult())) { filteredList.add(p); } } System.out.println(filteredList); } public static List<Person> getPersonList() { List<Person> personList=new ArrayList<>(); Person p1 = new Person("John",21,"Male"); Person p2 = new Person("Martin",17,"Male"); Person p3 = new Person("Mary",16,"Female"); Person p4 = new Person("Amy",25,"Female"); personList.add(p1); personList.add(p2); personList.add(p3); personList.add(p4); return personList; } } |
Output:
As you can see we are able to achieve the condition with help of &&
and ||
operators but if statement looks quite verbose.
1 2 3 |
if((p.isMale() && !p.isAdult()) || (!p.isMale() && p.isAdult())) |
You can use XOR
operator to achieve the same.
1 2 3 |
if((p.isMale() ^ p.isAdult())) |
Replace if((p.isMale() && !p.isAdult()) || (!p.isMale() && p.isAdult()))
with if((p.isMale() ^ p.isAdult()))
and you should be able to achieve same results.
BitWise XOR operator
XOR operator
works with primitive type as well. Bitwise XOR operator will always return output as 1 if either of its input 1 but not both.
Here is truth table for BitWise XOR operator.
P | Q | P XOR Q |
---|---|---|
1 | 1 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
0 | 0 | 0 |
Let’s see this with the help of example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package org.arpit.java2blog; public class BitWiseXORMain { public static void main(String[] args) { int i1 = 2; int i2 = 3; System.out.println("XOR of 2 and 3: " + (i1 ^ i2)); } } |
Output:
Let’s understand how XOR of 2 and 3 is 1.
As you can see, XOR operator returns output as 1 if either of its input 1 but not both.
That’s all about XOR operator in java.