Home » Core Java » Java XOR Operator Example

About Yatin

The author is graduated in Electronics & Telecommunication. During his studies, he has been involved with a significant number of projects ranging from programming and software engineering to telecommunications analysis. He works as a technical lead in the information technology sector where he is primarily involved with projects based on Java/J2EE technologies platform and novel UI technologies.

Java XOR Operator Example

Hello readers, in this tutorial, we will learn the Java XOR operator.

1. Introduction

Java programming offers Operators that are used to perform various operations in Java. In this tutorial, we’ll only cover the XOR operator that falls under the Bitwise category.

1.1 XOR operators in Java

The XOR operator is denoted as ^ in the programming language and works on bits of numbers (i.e. 0 or 1). It returns true if either of the bits in an operand is a 1. Following table summarizes the XOR operations –

Element 1Element 2Element 1 ^ Element 2

To start with the captioned tutorial, we are hoping that users at present have their preferred Ide installed on their machines. For easy usage, I am using Eclipse Ide on a Windows operating system.

2. Java XOR Operator Example

In this example, we’ll demonstrate the XOR operator in java. For a better understanding, developers can execute the below code in Eclipse Ide.


package com.jcg;
public class Example {
    // Bitwise XOR Operator - Such that either of the bits in an operand is '1', then the resultant bit is '1'.
    public static void main(String[] args) {
        int ele1 = 10;      // Binary format= 1010
        int ele2 = 20;      // Binary format= 10100
        // Bitwise XOR
        // 1010 ^ 10100 = 11110 = 30
        System.out.println("XOR Result= " + (ele1 ^ ele2));

If everything goes well, the following output will be printed in the Ide console.


XOR Result= 30

2.1 Output Explanation

In here,

  • In Binary format – 10 is represented as 1010
  • In Binary format – 20 is represented as 10100
  • According to the above table, we will get 11110 as a result
  • The println() method will print the decimal equivalent of the result

That is all for this tutorial and I hope the article served you whatever you were looking for. Happy Learning and do not forget to share!

3. Conclusion

In this tutorial, we learned the XOR operator in Java. Developers can download the sample application as an Eclipse project in the Downloads section.

4. Download the Eclipse Project

This was an example of XOR in Java programming language.

You can download the full source code of this example here: Java XOR Operator Example
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Leave a Reply

Notify of