Java Bitwise XOR operator:

In Java, you can use the ^ operator to perform a bitwise exclusive or (XOR) operation on two integers. The XOR operation compares each bit of the first number to the corresponding bit of the second number. If the bits are the same, the corresponding result bit is set to 0.  
The matching result bit is set to 1 if the bits vary.
Here's an example:

int a = 12; // binary: 1100
int b = 25; // binary: 11001
int result = a ^ b;
System.out.println("Result of XOR operation: " + result);

In this example, the XOR operation compares the bits of the integers a and b. Since the bits at positions 2 and 3 are different, the corresponding result bits are set to 1. Therefore the binary representation of result is 01101 which decimal representation is 13
This will output:

Result of XOR operation: 13

It's important to note that XOR operation is not only limited to integers. You can also perform it on bytes or even on bytes of an array, by applying the operation on each element of array.


byte[] array1 = {0, 1, 2, 3, 4, 5};
byte[] array2 = {5, 4, 3, 2, 1, 0};
byte[] result = new byte[6];

for (int i = 0; i < 6; i++) {
    result[i] = (byte) (array1[i] ^ array2[i]);
}

It's also useful for some cryptographic algorithms for example the One Time Pad encryption, where the XOR operation is used to encrypt and decrypt the data.
  • Java bit wise Xor operator operates on bits of numbers.
  • It will be represented as "^".
  • XOR will return true if  both bits are different.
  •  For example 1 XOR 0 gives 1. Ex: 1^0=1.
  • 0^1=1.




Java Bitwise Xor:

Java bitwise XOR operator


Program#1:Java Example program on two integer numbers Xor operation.


  1. package com.Xorinjava;
  2. public class XOR {
  3.     /**
  4.     * Xor in java Xor example program
  5.     * @author www.instanceofjava.com
  6.     */
  7.     public static void main(String[] args) {
  8.         int a=1;
  9.         int b=0;
  10.         int c= a^b;
  11.         
  12.         System.out.println(c);
  13.  
  14.     }
  15.  
  16. }
Output:

  1. 1

Program #2: java xor boolean expression : java example program


  1. package com.Xorinjava;
  2.  
  3. public class XOR {
  4.     /**
  5.     * Xor in java boolean Xor example program
  6.     * @author www.instanceofjava.com
  7.     */
  8. public static void main(String[] args) {
  9.         boolean a=true;
  10.         boolean b=false;
  11.         boolean c= a^b;
  12.         
  13.         System.out.println(c);
  14.         
  15.         System.out.println(false^false);
  16.         System.out.println(true^true);
  17.  
  18.     }
  19.  
  20. }

Output:


  1. true
  2. false
  3. false
Program #3: java xor boolean expression and integer : java example program Using Eclipse IDE.

java Xor operator

Java Xor Strings:

  • Gives compile time error.
  • The operator ^ is undefined for the argument type(s) java.lang.String, java.lang.String

Program #4: Java Xor of two strings


  1. package com.Xorinjava;
  2. public class XOR {
  3.     /**
  4.     * Xor in java boolean Xor example program
  5.     * @author www.instanceofjava.com
  6.     */
  7.     public static void main(String[] args) {
  8.         String str="a";
  9.         String str1="b";
  10.         System.out.println(str^str1);// compile time error: The operator ^ is undefined for the
  11. argument type(s) java.lang.String, java.lang.String
  12.  
  13.     }
  14.  
  15. }


 Xor of Binary Strings:

Program #5: Java xor of two binary strings.


  1. package com.Xorinjava;
  2. public class XOR {
  3.     /**
  4.     * Xor in java String bits Xor example program
  5.     * @author www.instanceofjava.com
  6.     */
  7.     public static void main(String[] args) {
  8.         String str1="1010100101";
  9.         String str2="1110000101";
  10.         StringBuffer sb=new StringBuffer();
  11.         
  12.         for (int i = 0; i < str1.length(); i++) {
  13.             
  14.             sb.append(str1.charAt(i)^str2.charAt(i));
  15.             
  16.         }
  17.        System.out.println(sb);
  18.     }
  19.  
  20. }
Output:


  1. 0100100000

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu