xor eax,3 why does the input change the way it does?

I am new to reverse engineering and I am trying to figure out exactly what xor is doing in this little program. I know if I put any number other than 0 I get a xor eax,3 so if I put in 1 I get 2 if I input 2 I get 1 if I input 7 I get 4 I am just trying to understand why. • Hi Landon! First impression: you are stripping relevant parts needed for further inference. Basically it's an conditional xor (3/2). Mar 1 '20 at 10:08
• @kn0x yes I am just trying to figure out what exactly xor does. Is there more info you need to answer that question? I can get the function before the jne. But basically it's just checking if the int entered by the user == 0 if it is then it goes to the xor ecx,2 if not then it does xor eax,3 on whatever number the user entered. Mar 1 '20 at 19:40
• if(abc) {x = x^3;printf("%d\n",x)} else {x = x ^2;printf("%d\n",x)} Mar 1 '20 at 19:59

In C, this function would look like this:

int fun()
{
int a;
// some code you haven't pasted here; probably scanf("%d", &a);...
if (some_condition)
a ^= 3; // xor a with 3
else
a ^= 2; // xor a with 2
printf("a = %d.\n", a);
return 0;
}

I cannot say anything more about it having only the snipped you shared with us. If there is some magic, it is contained in the part you haven't uploaded.

• This is very helpful. I am just trying to understand what xor does exactly. I assume its the a ^= 3 part? I am not familier with ^= could you add that part into your answer and then I think you have answered my question. Thanks! Mar 1 '20 at 19:39
• Yes a ^= 3 stands for a = a^3, that is a xor 3. I have updated my answer. Basically, xor with 2 changes second least significant bit to its negation, and xor with 3 changes both first and second least significant bit of a. They are just bit operations that act like sum modulo 2 on each bit. You can read more about xor here. Mar 1 '20 at 19:43