SoFunction
Updated on 2025-04-15

Detailed explanation of the XOR operation of Java difficult interpretation

Preface

In Java,XOR operation(XOR, symbol is^) is one of the bit operators that compare each bit of two integers and perform an "exclusive OR" operation. It has important uses in many application scenarios, such as encryption, checksum, digital image processing, etc.

1. Definition of XOR (XOR) operation

Exclusive OR (XOR, Exclusive OR) is a logical operation, and its rules are very simple:

  • Two bits are the same(i.e. 0 XOR 0 or 1 XOR 1), the result is 0.
  • Two bits are different(i.e. 0 XOR 1 or 1 XOR 0), the result is 1.

That is, the rule of XOR can be expressed by the following truth table:

A B A ^ B
0 0 0
0 1 1
1 0 1
1 1 0

2. The XOR operator in Java

In Java,^is the XOR operator, used to perform bit-level XOR operations. It can act on integers (intlongetc) type of data.

3. Basic usage

Suppose we have two integersaandb, we can use^The operator performs an exclusive OR operation. Here is a simple example:

public class XORExample {
    public static void main(String[] args) {
        int a = 5;  // Binary representation: 0101        int b = 3;  // Binary representation: 0011
        int result = a ^ b;  // Perform XOR operation
        ("a ^ b = " + result);  // Output result: 6    }
}

explain:

  • a = 5The binary representation of   is0101
  • b = 3The binary representation of   is0011
  • a ^ bXOR operations will be performed bit by bit:
    • 0 ^ 0 = 0
    • 1 ^ 0 = 1
    • 0 ^ 1 = 1
    • 1 ^ 1 = 0

therefore,a ^ b = 0110,Right now6

4. Characteristics of XOR operation

XOR operation has some very useful mathematical properties that make it very useful in various algorithms.

4.1. Reflexivity:

a ^ a = 0Any number performs an exclusive OR operation with itself, and the result is 0.

4.2. Units:

a ^ 0 = aAny number performs an exclusive OR operation with 0, and the result is the number itself.

4.3. Exchange law:

a ^ b = b ^ aThe XOR operation satisfies the commutation law and the order can be exchanged.

4.4. Combination law:

(a ^ b) ^ c = a ^ (b ^ c)The XOR operation satisfies the law of bonding, that is, it is possible to ignore the order of brackets.

4.5. Reverse Yuan:

a ^ b = c,Soc ^ b = aIf the result of two numbers exclusively OR isc, thencExclusive OR with one of the numbers, the result is another number.

5. Common application scenarios

5.1. Exchange the values ​​of two variables

Exor can be used to exchange the values ​​of two variables without the need to use temporary variables:

public class XORSwap {
    public static void main(String[] args) {
        int x = 5;
        int y = 3;

        // Use XOR to exchange values        x = x ^ y;
        y = x ^ y;
        x = x ^ y;

        ("x = " + x);  // x = 3
        ("y = " + y);  // y = 5
    }
}

explain:

  • x = x ^ yxSavedxandyThe result of XOR.
  • y = x ^ y:at this time,yRestored to the originalx
  • x = x ^ y:final,xRestored to the originaly

This completes the exchange of two variables.

5.2. Check whether the two numbers are equal

If two numbersaandbThe result obtained by the XOR operation is 0, then they are equal. Otherwise, they are not equal.

public class XOREquality {
    public static void main(String[] args) {
        int a = 5;
        int b = 5;
        
        if (a ^ b == 0) {
            ("a and b are equal");
        } else {
            ("a and b are not equal");
        }
    }
}

5.3. Find numbers that only appear once

XOR operation is also often used to solve the problem of "finding numbers that only appear once". For example, in an array, all elements appear in pairs, and only one element appears once. This unique element can be found through the XOR operation.

public class FindUnique {
    public static void main(String[] args) {
        int[] nums = {4, 3, 2, 4, 1, 3, 2};
        
        int result = 0;
        for (int num : nums) {
            result ^= num;  // Extraordinate all numbers        }
        
        ("The only number is: " + result);
    }
}

explain:

  • In this example,resultThe only number will be stored in the end, because the paired numbers will cancel each other after XOR, and the rest is the non-repeat number.

6. Summary

The XOR operator in Java (^) is very useful in bit operations. It compares the binary bits of two numbers bit by bit, if the same, the result is 0, and if different, it is 1. Its important characteristics include reflexivity, exchange law, binding law, etc. These characteristics make XOR very useful in many algorithms, especially in issues such as exchange variables, checking equality, and finding unique numbers.

This is the end of this article about the interpretation of the difficult Java XOR operation. For more related Java XOR operation content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!