Contest Duration: - (local time) (100 minutes) Back to Home
Official

## A - Bitwise Exclusive Or Editorial by en_translator

If you are new to learning programming and do not know where to start, please try Problem A “Welcome to AtCoder” from practice contest. There you can find a sample code for each language.

#### Using the property of XOR

If the theme is an addition instead of XOR, that is, if the problem is “find $$C$$ such that $$A+C=B$$,” then we can subtract $$A$$ from both sides to obtain $$C=B-A$$. What is the operation in XOR corresponding to “subtracting $$A$$ from both sides” in the case of addition?

Actually, for any non-negative integer $$A$$, we have $$A \ XOR \ A=0$$. Therefore, by XOR-ing $$A$$ to both sides, the $$A$$ in the left hand side is eliminated, and now we have $$C=B \ XOR \ A$$ that enables us to calculate $$C$$.

In many languages, the ^ operator is served for XOR computation.

Sample code (C)

#include<stdio.h>

int main(){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",a^b);
}


Sample code (Python)

A,B = map(int, input().split())
print(A^B)


#### Brute-forcing

We know that the answer is one of the $$256$$ integers in the range of $$0$$ and $$255$$. We can solve the problem by checking all the $$256$$ cases if the equivalence is satisfied.

When implementing, you have to be careful of the precedence of operators.

Sample code (C)

#include<stdio.h>

int main(){
int a,b;
scanf("%d%d",&a,&b);
for(int c=0;c<=255;c++){
if((a^c)==b){
printf("%d\n",c);
return 0;
}
}
}


Sample code (Python)

A,B=map(int,input().split())
for C in range(256):
if (A^C)==B:
print(C)
exit()


posted:
last update: