Explain Codes LogoExplain Codes Logo

What is the inverse function to XOR?

java
xor-algorithms
bit-manipulation
problem-solving
Nikita BarsukovbyNikita Barsukov·Mar 11, 2025
TLDR

The quick and dirty answer is: XOR is its own inverse. Yes, you heard it right! Use XOR twice with the same operand and it turns back into the original value:

int a = 5; // Binary: 101 (What's a pirate's favorite number? "101" – because one-eyed!) int b = 3; // Binary: 011 int xor = a ^ b; // Returns 6; Binary: 110 (1s and 0s singing: "I've got the power!") int originalA = xor ^ b; // Returns 5, back to Binary: 101 (Hello old friend, we meet again!)

originalA is a. XOR here showcased its own undo button.

The nullifying effect of XOR

The real strength of the XOR operation lies in its nullifying effect. With only the result and one original input, the other input can be unmasked using the known original and the result:

int result = 6; // Binary: 110 (The binary version of evil number: 666!) int knownInput = 3; // Binary: 011 int missingInput = result ^ knownInput; // Reveals 5, Binary: 101 (Abracadabra, 5 reappears)

And voilà, missingInput is the other original input. This feature makes XOR the master key to cryptography and checksum algorithms!

Unmasking a range of numbers

What if we want to find a range of numbers given a result and one input value? XOR can help light the candle in our search party:

int result = 6; // The 'known' result for(int i = 0; i <= result; i++){ int possibleMatch = result ^ i; System.out.println("Paired with " + i + ", the result was " + possibleMatch); }

The loop serves up every possible combination of number pairings between i and the result, retrieving the complete spectrum of integers that jointly lead to the output value.

XOR's page in Leetcode's book

Looking for some hands-on practice? Leetcode has some problems that will rev up your XOR engine. 1720: Decode XORed Array and 2433: Find the Original Array of Prefix Xor will challenge your understanding of XOR's retroactive power and help flex your problem-solving muscles.

XOR - the secret sauce in our culinary code

Knowing that XOR can bring back hidden values gives us a scrumptious ingredient to add to our culinary code. In the kitchen of data validation and disguised information, XOR is the sous-chef that makes delicious solutions possible.

Exploring the symmetry of XOR

The combo move of XOR, x ^ y, can be twisted to y ^ x and the output still doesn't change. That's XOR's mirroring behavior at play:

int x = 2; // Unary: 10 int y = 5; // Also Unary: 101 (Unary or binary? That's the question!) System.out.println(x ^ y); // Reveals 7, Unary: 111 System.out.println(y ^ x); // Behold! Also 7, Unary: 111

This symmetric trait of XOR helps grasp how operations can be reversible in the XOR universe.

Spotlight on XOR algorithms

Have you heard about the XOR Linked List that shaves off memory usage? Or the XOR Swap Algorithm where the values swap places without a temp variable?

//Here's a taste of XOR's magic: int x = 10; int y = 20; x = x ^ y; // X now has the "XOR-ed" blend of X and Y y = x ^ y; // XOR throws away the X part and leaves us with Y x = x ^ y; // XOR ditches the Y part and we find X // Magic swap competed: x = 20, y = 10

Such XOR quirks take our solution strategies down unconventional but efficient alleys.