Home Java Master-java Java Bitwise XOR "^" Operator

Related Tutorials


 
 

Share on Google+Share on Google+

Java Bitwise XOR "^" Operator

Advertisement
In this section, you will learn how to use bitwise XOR "^" operator in Java.

Java Bitwise XOR "^" Operator

     

In this section, you will learn how to use bitwise XOR "^" operator in Java. The Java programming language has operators that perform bitwise operations. In the example below we have shown the usage of  bitwise XOR "^" operator.

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.

In the program code given below, the corresponding bits of both operands are 1 and 0, hence we get 1 as output because the bits are different. 

Here is the code of program: 

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

Output of the program:

C:\unique>javac BitwiseXOR.java

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

C:\unique>

Download this example.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 8, 2007

Related Tutorials

Discuss: Java Bitwise XOR "^" Operator   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:24
tkj tkj
June 27, 2012
error in your article

re: ".. 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 last statement is not correct: 'XOR' return 1 only if both items are different. If they are the same, it returns "0" .
TheGov
July 5, 2012
This is wrong!

<quote> Moreover if both of the bits are 1 i.e. 1^1 then also it produces 1. </quote> This is incorrect! If both of the input bits are 1 (i.e 1^1), then the result will be 0!
David Vernet
August 5, 2012
Incorrect application of XOR

XOR is only true (=1) if ONLY one of the two values being XOR'd is equal 1. In other words, XOR = true iff ONE value = true. In your code you have 1^1 = 1 which is incorrect.
Dan Nissenbaum
August 8, 2012
THIS ANSWER IS INCORRECT

I am shocked, but this answer is incorrect (as of Aug 7, 2012). 'XOR' does not return 1 if both bits are 1. Quoting the answer: "Moreover if both of the bits are 1 i.e. 1^1 then also it produces 1." This is INCORRECT. If both of the bits are 1, then XOR produces 0. See, i.e., http://en.wikipedia.org/wiki/Xor.
Anil Gupta
August 10, 2012
1^1 is equal to 0 not 1

Hi, 1^1 is equal to 0 not 1. Please fix the mistake. It gives incorrect information about XOR operation.
Steven B
August 10, 2012
Dealing with the correction and comments of XOR

Java's XOR is bit XOR, meaning each 1 or 0 is checked. Therefore if you have 2 ^ 3, that's actually 0b10 XOR 0b11 which then equals 0b01, or in decimal, 1. As all have said, 1 ^ 1 = 0. X stands for exclusive, so if two values are the same, they aren't exclusive, therefore false, or 0. This is the core difference between OR and XOR.
John Smith
October 17, 2012
Incorrect information

1^1 will produce zero. Exclusive OR means a value of 1 is only given if one bit is 0 and the other is 1. This page really added to my confusion when researching this topic.
Lars
November 16, 2012
Erroneous description

Please remove the text that says: "Moreover if both of the bits are 1 i.e. 1^1 then also it produces 1.". This is not correct, it produces 0 just as the first text says: "The bitwise XOR "^" operator produces 1 if both of the bits in its operands are different."
sijin mary
April 12, 2013
Request code for following algorithm steps

1. Initially select plane text of 16 bytes (or we can vary from 16 to 64 depend on requirement). 2. Initially insert key of size 16 bytes ( depend on plane text value) 3. Apply XOR operation between key (Key_Block4213) and plane text block (Text_Block). Result will store in Cipher Block1. 4. Apply right circular shift with 3 values. Result will store in new Cipher_Block2. 5. Apply XOR operation between Cipher_Block2 and Key_Block2. Result will store in new Cipher_Block3. 6. Apply XOR operation between Cipher_Block3 and Key_Block4. Result will store in Cipher_Block4. 7. Cipher_Block4 is the input of the next round as a plane text block. 8. Repeat step 1 to 7 till (Encryption Number / 4). 9. Exit
DMCA.com