Java Exclusive OR Operator
Java Exclusive OR Operator
In this tutorial, we will learn about Java Exclusive OR operator with a simple Java program. The Exclusive OR( XOR ) operator produces 1 where the bits in its operands are different and produces 0 if they are the same.
In Java, the Exclusive OR (XOR) operator is represented by the caret symbol. The ^ is the exclusive OR operation symbol.
Table
Outcome table with the Bitwise Exclusive OR operator
Operand: A | Operand: B | Exclusive OR : A ^ B |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
The XOR operator compares two boolean or integer values and returns:
-
true
(or1
) if the values are different -
false
(or0
) if the values are the same
Example Program
/ * * Java Program to demonstrate * Bitwise Exclusive OR(XOR) Operator. * Java Tutorials -- www.TestingDocs.com */ public class BitwiseExclusiveORDemo { public static void main(String args[]) { //Declare two variables int A = 1; int B = 0; System.out.println("A ^ B =: " + (A ^ B)); A = 0; B = 0; System.out.println("A ^ B =: " + (A ^ B)); } }
Program Output
A ^ B =: 1
A ^ B =: 0
—
Java Tutorials
Java Tutorial on this website: