# Thread: Explain java xor (^) operator

1. Member
Join Date
Apr 2008
Posts
30

## Explain java xor (^) operator

Hi,

I am just a bit confused using XOR (^) in java. Is there any example for using or explaining this operator?

2. Member
Join Date
May 2008
Posts
24

## Re: Explain java xor (^) operator

xor

^ is both a both boolean and bitwise operator in Java. It gives the bitwise difference. e.g.

Code:
0b1100_0000 ^ 0b1010_0001 -> 0b0110_0001
The expression a ^ b is true if either a or b is true but not if both are true. We call this the exclusive or. In contrast the expression a | b is true if either a or b is true or both are true. We call this the inclusive or or the lawyer’s and/or.

Conditional Toggle

Consider a conditional toggle like this:

Code:
if ( condition )
{
toggle = ! toggle;
}
You can code it more tersely like this:

Code:
toggle ^= condition;
Further, the generated byte code for the xor version will be more compact, and the generated machine code will execute more quickly.

3. Member
Join Date
May 2008
Posts
44

## Re: Explain java xor (^) operator

Java Bitwise XOR "^" Operator

The Java programming language has operators that perform bitwise operations.
Description of code:

The bitwise XOR "^" operator produces 1 if both of the bits in its operands are different. However, if both of the bits are same then this operator produces 0. Moreover if both of the bits are 1 i.e. 1^1 then also it produces 1.
The corresponding bits of both operands are 1 and 0, hence we get 1 as output because the bits are different.

Code:
class BitwiseXOR{
public static void main(String args[]){
System.out.println(" ^ XOR operator");
int x = 1 ^ 0;
System.out.println("1 ^ 0 = " + x);
}
}
Output:

Code:
C:\unique>javac BitwiseXOR.java

C:\unique>java BitwiseXOR
^ XOR operator
1 ^ 0 = 1

C:\unique>

4. Member
Join Date
May 2008
Posts
21

## Re: Explain java xor (^) operator

Graphic Example

Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class XORRectangles extends JPanel{

public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;

// using white as the XOR color.
g2.setXORMode(Color.white);
// Paint a red rectangle.
Rectangle2D r = new Rectangle2D.Double(50, 50, 150, 100);
g2.setPaint(Color.red);
g2.fill(r);
g2.transform(AffineTransform.getTranslateInstance(25, 25));
// Draw a blue rectangle.
g2.setPaint(Color.blue);
g2.fill(r);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(300, 200);
f.setVisible(true);
}
}

5. Member
Join Date
Jan 2006
Posts
605

## Re: Explain java xor (^) operator

The operator ^ performs the exclusive OR operation bit (XOR). XOR function applied on each pair of bits of equal weight is to each operand. The XOR function is evaluated to true if the operands have the same value. Finally, the complement operator inverts the value of each bit of the operand. In. NET Framework, the equivalent class, System.Decimal, can not perform logical operations. The XOR operator (^) can not be applied to an operand System.Decimal.

6. Member
Join Date
May 2009
Posts
543

## Re: Explain java xor (^) operator

The bitwise XOR "^" operator in JAva is use to perform bitwise operations. The operator here helps you to perform bitwise and bit shift operations. This are less used in java. So it is recommended that you must go with a brief description of this. There are sample script and files on web that you can try to learn about this.