Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , ,

Sponsored Links


Explain java xor (^) operator

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 30-07-2009
Member
 
Join Date: Apr 2008
Posts: 30
Explain java xor (^) operator

Sponsored Links
Hi,

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

Thanks in advance.

Reply With Quote
  #2  
Old 30-07-2009
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.
Reply With Quote
  #3  
Old 30-07-2009
Xeusion's Avatar
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>
Reply With Quote
  #4  
Old 30-07-2009
Janus's Avatar
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.getContentPane().add(new XORRectangles());
    f.setSize(300, 200);
    f.setVisible(true);
  }
}
Reply With Quote
  #5  
Old 03-05-2012
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.
Reply With Quote
  #6  
Old 09-05-2012
Member
 
Join Date: May 2009
Posts: 514
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Explain java xor (^) operator"
Thread Thread Starter Forum Replies Last Post
The way to operator overloading in Java Kohlmann Software Development 8 18-09-2010 09:57 PM
Explain main method in java Angelica Maria Software Development 5 10-03-2010 12:18 PM
How to use the ternary operator in java program? Kasper Software Development 4 23-01-2010 06:08 PM
Explain Try Catch Block in java cyber-noob Software Development 3 16-11-2009 01:13 PM
explain class in java GlassFish Software Development 3 03-11-2009 08:59 AM


All times are GMT +5.5. The time now is 09:31 AM.