Bitwise operations and Logical operations using XOR
- XOR
- Properties
- Given an array of integers where every element appears twice except for one, find that single number.
- Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.
- Swapping numbers using bitwise operations
- Find two numbers from their sum and XOR
- Tips for solving XOR problems
XOR
^ - bitwise exclusive OR - bitwise XOR
Properties
A XOR B COR A = B
Thus, if any value is added an even number of times to XOR operations, then this value is removed from the result.
Explanation using bits
e.g.
1 XOR 2
- 1 is represented as 01 in binary.
- 2 is represented as 10 in binary.
So, 1 XOR 2 is 01 XOR 10. This results in 11 - which equals decimal 3.
- If we do XOR with this result and
1,11 XOR 01=10, which is decimal 2. - If we do XOR with this result and
2,11 XOR 10=01, which is decimal 1.
Given an array of integers where every element appears twice except for one, find that single number.
Based on the explanation above, we can just run a loop on the array, and all the elements that appear twice in the array will be removed from the end result of the XOR.
By the end, the only remaining result will be the bit values for the element that appears in the array only once.
Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.
The result of XOR of first n natural numbers with the XOR of all the array elements will be the missing number. To do so, calculate XOR of first n natural numbers and XOR of all the array arr[] elements, and then our result will be the XOR of both the resultant values.
Swapping numbers using bitwise operations
The beauty of this method is, we do not have to worry about the datatype of the input variables. Or, their limits. We don’t have to worry about overflows. This will work in any language, including the ones that do not support overflows.
public static ImmutablePair swapUsingBitwiseManipulation(ImmutablePair pair) {
int a = (int) pair.left;
int b = (int) pair.right;
a = a ^ b;
b = a ^ b; // actually (a ^ b) ^ b, which would give us a -> assign this value to b.
a = a ^ b; // which is, (a ^ b) -((a ^ b) ^ b) = (a ^ b) ^ a = b -> assign this value to a.
return new ImmutablePair(a, b);
}
Find two numbers from their sum and XOR
TODO
- https://www.geeksforgeeks.org/dsa/find-two-numbers-sum-xor/
- https://stackoverflow.com/questions/36477623/given-an-xor-and-sum-of-two-numbers-how-to-find-the-number-of-pairs-that-satisf
Easy/Beginner:
- Single Number:
Given an array of integers where every element appears twice except for one, find that single number.
- Missing Number:
Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.
- Two Sum - XOR Version:
TODO
Given an array of integers and a target value, find two numbers in the array that add up to the target (using XOR to potentially optimize).
Intermediate:
- Maximum XOR of Two Numbers in an Array:
Given an array of integers, find the maximum result of XOR between any two numbers in the array.
- Subarray XOR Queries:
Given an array of integers and a list of queries, each query containing a start and end index, return the XOR of the elements in the subarray.
https://www.codechef.com/practice/course/1-star-difficulty-problems/DIFF1200/problems/MINMXOR
- The Great XOR:
Find the number of integers less than a given integer ‘x’ that have a bitwise XOR with ‘x’ resulting in a value greater than ‘x’.
- Interesting XOR:
Given an integer C, find the maximum product of A and B where A XOR B = C and A, B are less than 2d, where 2d is the smallest power of 2 greater than C.
https://www.codechef.com/practice/course/2-star-difficulty-problems/DIFF1500/problems/IRSTXOR
Advanced:
- Communication Complexity of XOR Function:
Analyze the communication complexity of the XOR function in different communication models.
- XOR Basis:
Find a minimal set of vectors that can generate all other vectors in a given set using XOR as the operation.
https://codeforces.com/blog/entry/68953#:~:text=In%20view%20of%20this%20plane,xor%20of%202%20and%203.
Tips for solving XOR problems
XOR’s Properties
Remember that A XOR A = 0, and A XOR 0 = A. Also, XOR is commutative and associative.
Bit Manipulation
XOR is often used for bit manipulation tasks, such as toggling bits or checking if a bit is set.
Look for Patterns
Many XOR problems involve finding a unique element in a set where all other elements are paired.
https://emre.me/coding-patterns/bitwise-xor/
Think in Binary
Converting numbers to their binary representation can help visualize the XOR operation.
Don’t be afraid to experiment with XOR on small examples
It can help you discover patterns that might not be obvious.