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:
Program#1:Java Example program on two integer numbers Xor operation.
- package com.Xorinjava;
- public class XOR {
- /**
- * Xor in java Xor example program
- * @author www.instanceofjava.com
- */
- public static void main(String[] args) {
- int a=1;
- int b=0;
- int c= a^b;
- System.out.println(c);
- }
- }
- 1
Program #2: java xor boolean expression : java example program
- package com.Xorinjava;
- public class XOR {
- /**
- * Xor in java boolean Xor example program
- * @author www.instanceofjava.com
- */
- public static void main(String[] args) {
- boolean a=true;
- boolean b=false;
- boolean c= a^b;
- System.out.println(c);
- System.out.println(false^false);
- System.out.println(true^true);
- }
- }
Output:
- true
- false
- false
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
- package com.Xorinjava;
- public class XOR {
- /**
- * Xor in java boolean Xor example program
- * @author www.instanceofjava.com
- */
- public static void main(String[] args) {
- String str="a";
- String str1="b";
- System.out.println(str^str1);// compile time error: The operator ^ is undefined for the
- argument type(s) java.lang.String, java.lang.String
- }
- }
Xor of Binary Strings:
Program #5: Java xor of two binary strings.
- package com.Xorinjava;
- public class XOR {
- /**
- * Xor in java String bits Xor example program
- * @author www.instanceofjava.com
- */
- public static void main(String[] args) {
- String str1="1010100101";
- String str2="1110000101";
- StringBuffer sb=new StringBuffer();
- for (int i = 0; i < str1.length(); i++) {
- sb.append(str1.charAt(i)^str2.charAt(i));
- }
- System.out.println(sb);
- }
- }
- 0100100000
No comments