It's not very hard, you just need to think small: suppose we are writing A
, B
and X
in binary and Aᵢ
is the value corresponding to the rightmost 2ⁱ bit.
We know that: Aₒ ⊕ Xₒ = Bₒ + Xₒ
.
Let's use an example to discover how to evaluate that: A = 15 and B = 6. Converting to binary:
A = 1 1 1 1 B = 0 1 1 0
X = a b c d X = a b c d
Now we have some possibilities. Let's analyse the rightmost bits of A and B:
1 ⊕ d = 0 + d
We know that d
can only be 0 or 1, so:
for d = 0
1 ⊕ d = 0 + d => 1 ⊕ 0 = 0 + 0 => 1 = 0 (not possible)
for d = 1
1 ⊕ d = 0 + d => 1 ⊕ 1 = 0 + 1 => 0 = 1 (not possible)
It's noticeable that XOR behaves just like binary sum (with the difference that XOR doesn't create a carryover for the next bit sum):
XOR SUM
0 ⊕ 0 = 0 | 0 + 0 = 0
0 ⊕ 1 = 1 | 0 + 1 = 1
1 ⊕ 0 = 1 | 1 + 0 = 1
1 ⊕ 1 = 0 | 1 + 1 = 0
so it won't be always possible to find a X that satisfies A ⊕ X = B + X
, because there isn't a value d
that satisfies 1 + d = 0 + d
.
Anyway, if X exists, you can just find it out this way, from right to left, finding bit by bit.
WORKING FULL EXAMPLE
A = 15, B = 7:
A = 1 1 1 1 B = 0 1 1 1
X = a b c d X = a b c d
1 ⊕ d = 1 + d
Here, both d = 0 and d = 1 apply, then what? We need to check the next bit. Suppose d = 1:
A = 1 1 1 1 B = 0 1 1 1
X = a b c d X = a b c d
1 ⊕ d = 1 + d => 1 ⊕ 1 = 1 + 1 => 0 = 0 (possible)
BUT 1 + 1 = 0 generates a carryover for the next bit sum:
Instead of 1 ⊕ c = 1 + c, we have 1 ⊕ c = 1 + c (+1) =
1 ⊕ c = c (not possible)
so in this case, d must be 0.
carryover 0
A = 1 1 1 1 B = 0 1 1 1
X = a b 0 0 X = a b 0 0
-----------------------------------
0 0
we know that c must be 0:
carryover 0 0
A = 1 1 1 1 B = 0 1 1 1
X = a b 0 0 X = a b 0 0
-----------------------------------
1 1 1 1
but what about b? we need to check the next bit, as always:
if b = 0, there won't be a carryover, so we'll have:
1 ⊕ a = 0 + a (and this is not possible)
so we try b = 1:
1 ⊕ b = 1 + b => 1 ⊕ 1 = 1 + 1 => 0 = 0 (with carryover)
and now, for a
:
carryover 1 0 0
A = 1 1 1 1 B = 0 1 1 1
X = a 1 0 0 X = a 1 0 0
-----------------------------------
0 0 0 0 0 0
1 ⊕ a = 0 + a (+1) => 1 ⊕ a = 1 + a
here a
can be 0 and 1, but it must be 0, in order to avoid a carryover in the sum B + X
.
Then, X = 0 1 0 0
, thus X = 4.
CODE
#include <iostream>
using namespace std;
inline int bit(int a, int n) {
if(n > 31) return 0;
return (a & ( 1 << n )) >> n;
}
int main(){
int A = 19;
int B = 7;
int X = 0;
int carryover = 0;
int aCurrent, aNext, bCurrent, bNext;
for(int i = 0; i < 32; i++){
aCurrent = bit(A, i); bCurrent = bit(B, i);
aNext = bit(A, i + 1); bNext = bit(B, i + 1);
if(aCurrent == 0 && bCurrent == 0){
if(carryover) {X = -1; break;}
if(aNext != bNext){
X += 1 << i;
}
carryover = 0;
}
else if(aCurrent == 0 && bCurrent == 1){
if(!carryover) {X = -1; break;}
if(aNext == bNext){
X += 1 << i;
}
carryover = 1;
}
else if(aCurrent == 1 && bCurrent == 0){
if(!carryover) {X = -1; break;}
if(aNext != bNext){
X += 1 << i;
carryover = 1;
}
else {
carryover = 0;
}
}
else if(aCurrent == 1 && bCurrent == 1){
if(carryover) {X = -1; break;}
if(aNext != bNext){
X += 1 << i;
carryover = 1;
}
else {
carryover = 0;
}
}
}
if(X != -1) cout<<"X = "<<X<<endl;
else cout<<"X doesnt exist"<<endl;
return 0;
}
You can test it here.
a xor b = a + b mod 2
. Try to think about that equivalence for a little while.mod 2
as in the mathematical (mod 2), i.e. 3 === 7 (mod 2). The point is that you can discover an equation for the first bit of X, then go on to the next bit where (respecting the carry) you get an equation for the second bit, etc., like Daniel's answer.