Home Java Master-java Java Bitwise XOR "^" Operator
Questions:Ask|Latest

 
 

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.

Advertisements

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

Ask Questions?    Discuss: Java Bitwise XOR "^" Operator   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
bitvolcano
March 30, 2011
Incorrect description

The above "Description of code:" looks wrong to me: "Moreover if both of the bits are 1 i.e. 1^1 then also it produces 1." Exclusive or is 0 if both bits are 1s. 1 ^ 1 == 0
ivo
May 10, 2011
Wrong statement of 1 ^ 1

Hello, I just test the mentioned above with the following : System.out.println(0 ^ 1); System.out.println(1 ^ 0); System.out.println(1 ^ 1); System.out.println(0 ^ 0); The output of these lines is : 1 1 0 0 And obviously 1 ^ 1 gives 0 not 1 as mentioned above.
usman
June 3, 2011
xor on strings

Hi all, how can we perform bitwise xor operation on strings?
Ander
August 19, 2011
Error in your post

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. ------------------ This is not true. XOR operator in Java always output this: false ^ false = false false ^ true = true true ^ false = true true ^ true = false
M
August 30, 2011
Article shows wrong result for 1^1

This sentence is completely wrong: "Moreover if both of the bits are 1 i.e. 1^1 then also it produces 1." It should say that: If both of the bits are 1 i.e. 1^1 then it produces *0*.
Ezra Qu
February 19, 2013
False information

"Moreover if both of the bits are 1 i.e. 1^1 then also it produces 1." is false //BECAUSE class BitwiseXOR { public static void main(String args[]) { System.out.println(" ^ XOR operator"); int x = 1 ^ 1; System.out.println("1 ^ 1 = " + x); } } //ALWAYS PRODUCES 1 ^ 1 = 0
mjoraid
October 17, 2011
how?

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. I didnt the third part, is't it contradicting with the second one ???
Michal
November 11, 2011
1^1 = 0

I'm pretty sure there is a mistake.When you xor 1^1, you get 0. >>...Moreover if both of the bits are 1 i.e. 1^1 then also it produces 1.
Jason Moore
February 6, 2012
Error in XOR explanation

It states: "Moreover if both of the bits are 1 i.e. 1^1 then also it produces 1." This is not correct. The last number should be 0 rather than 1.
Andrey Vetlugin
February 7, 2012
such a shame

Such a shame that this is the top result on google "java xor". "Moreover if both of the bits are 1 i.e. 1^1 then also it produces 1." The author can go and kill himself for trying to explain things he/she doesn't know.
Bhagya
February 14, 2012
1^1

1^1 gives 0 as output. I doubt the above mentioned behavior (Moreover if both of the bits are 1 i.e. 1^1 then also it produces 1.) is true.
Bastard
February 21, 2012
FAIL

"Moreover if both of the bits are 1 i.e. 1^1 then also it produces 1." WRONG! 1^1 = 0
Trent
March 15, 2012
Correction

Bitwise XOR (aka. exclusive OR) sets the result to 1 if the bits are different and 0 if the bits are the same. Your description; "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." describes a bitwise OR operation. If you'd made an example program with all 4 possibilities, you would have seen this. :)
Salim boukerma
April 20, 2012
xor operator (^).

1^1 does not produces 1, it produces 0 unlike you said in your example.
John Schudy
May 10, 2012
Mistake in your page

On this page you have written: " Moreover if both of the bits are 1 i.e. 1^1 then also it produces 1." This is incorrect for an XOR operation. It would be correct for the regular OR but not the eXclusive OR.
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