Emilia and Bitwise Xor
Problem Statement
Emilia is playing with bitwise xor. She is given integers A and B between 0 and 255 (inclusive).
Help her find a non-negative integer C such that A xor C = B => (A⊕C = B).
It can be proved that there uniquely exists such C, and it will be between 0 and 255 (inclusive).
Help her find a non-negative integer C such that A xor C = B => (A⊕C = B).
It can be proved that there uniquely exists such C, and it will be between 0 and 255 (inclusive).
Input
The input consists of two space separated integers A and B.
Constraints
Constraints
0 ≤ A, B ≤ 255
All values in input are integers.
Solution:-
So in this problem we have to find the value of C. We have given the values of A and B and the values of A and B are in between 0 to 255. using this we have to find the value of C like - by finding the XOR of A and B we can simply find the value of C. We have given the equation A ⊕ C = B. We have to transform this equation first as C = A ⊕ B by applying XOR A to both sides like - A ⊕ A ⊕ C = A ⊕ B ==> So here by applying the rule of XOR we can transform the above equation to C =A ⊕ B.
So the value of C is also in between 0 to 255.
Example to transform the equation.
A ⊕ C = B ==> Apply the XOR A to both sides
A ⊕ A ⊕ C = A ⊕ B ==> so by applying the rule of xor the equation is
0 ⊕ C = A ⊕ B ==> Same digits returns 0 and different digits return 1.
Final equation is - C = A ⊕ B.
By using this we can find the value of any variable like - A, B and C as well.
See below how we can find the value of any variable.
A = B ⊕ C ==> To find the value of A.
B = A ⊕ C ==> To find the value of B.
C = A ⊕ B ==> To find the value of C.
Like this we can find the XOR of any variable.
To solve this problem we have to use the property of XOR. First what we can do will convert the given number into it's binary representation and then we apply the XOR property to this binary numbers.
So XOR property states Same digits will return 0 and different digits return 1.
Let's see the properties of XOR:-
1 1 ----> 0
0 1 ------> 1
1 0 -----> 1
0 0 ------> 0
Lets take one example
A = 10 and B = 12 and we have to find the C.
C = A ⊕ B
A = 1 0 1 0 --- 10
B = 1 1 0 0 ---- 12
C = 0 1 1 0 ---- 6 Value of C.
So the value of C is 6 by applying XOR property on 10 and 12.
Approach: -
By using XOR operator:-
We can solve this by using XOR operator. so the XOR operator is represented as ( ^ ). simply we can do A ^ B to solve this problem.
Without using XOR Operator:-
We can solve it without using XOR operator as well. so simply we can do like (a | b) - (a & b ).
Solution:-
Java Code:- Without using XOR (^) operator.
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
// Your code here
Scanner sc=new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = ((a | b) - (a & b));
System.out.print(c);
}
}
Using XOR operator:-
class Main {
public static void main (String[] args) {
// Your code here
Scanner sc=new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = a ^ b;
System.out.print(c);
}
}
C++ Code:-
#include <bits/stdc++.h> // header file includes every Standard library
using namespace std;
int main() {
// Your code here
unsigned int A,B,C;
cin>>A>>B;
cout<<(A^B);
return 0;
}
JavaScript Code:-
// Your code here
const input = require('fs').readFileSync('/dev/stdin', 'utf-8').split('\n')
const [A, B] = input[0].split(' ').map((v) => parseInt(v))
console.log(A ^ B);
Python Code:-
# Your code here
a,b = map(int,input().split())
print(a^b)
If you liked this article, share it with your friends and colleagues!
No comments: