Results 1 to 6 of 6

Thread: Explain java xor (^) operator

  1. #1
    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?

    Thanks in advance.

  2. #2
    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. #3
    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. #4
    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);
      }
    }

  5. #5
    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. #6
    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.

Similar Threads

  1. The way to operator overloading in Java
    By Kohlmann in forum Software Development
    Replies: 8
    Last Post: 18-09-2010, 09:57 PM
  2. Explain main method in java
    By Angelica Maria in forum Software Development
    Replies: 5
    Last Post: 10-03-2010, 01:18 PM
  3. How to use the ternary operator in java program?
    By Kasper in forum Software Development
    Replies: 4
    Last Post: 23-01-2010, 07:08 PM
  4. Explain Try Catch Block in java
    By cyber-noob in forum Software Development
    Replies: 3
    Last Post: 16-11-2009, 02:13 PM
  5. explain class in java
    By GlassFish in forum Software Development
    Replies: 3
    Last Post: 03-11-2009, 09:59 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,698,194.94361 seconds with 17 queries