It took me a while to figure out what you were trying to say here. The symbol "^" is usually:
1) used for exponent.
2) or for the "and" function in context of binary variables.
I am assuming you used it for exclusive-or / xor.
Here's a verification of this method:
to avoid confusion lets use the letters A and B for the initial "binary" values before the execution of these commands.
1) a = a xor b
after this command:
a = A xor B
b = B
2) b = a xor b
after this command:
a = A xor B
b = (A xor B) xor B = A xor (B xor B) = A xor 0 = A
3) a = a xor b
after this command:
a = (A xor B) xor A = A xor (B xor A) = A xor (A xor B) = (A xor A) xor B = 0 xor B = B
b = A
So this method does work.
But I am interested in seeing what would be a good way for 'arriving' at this method. Like, for example, several simple math equalities ( say (n+1)^2 = n^2 + 2n + 1 ) you can 'prove' by induction. But usually arriving at those equalities is easier through some other insight (like simple algebraic multiplication in my example).
Secondly can this exchange be done through other binary functions too (and which ones)?
---
At any rate, I gave it some more thought. For variables supposed to represent rational and real nos. I think the following method should work:
for the case b>a do the following
1) a = a + (b-a)/2
2) b = a - (b-a)
3) a = a + (a-b)
Lets assume the initial "numeric" values for variables "a" and "b" to be "A" and "B" before the execution of these commands.
1) a = a + (b-a)/2
after this command:
a = A + (B-A)/2
b = B
2) b = a - (b-a)
after this command:
a = A + (B-A)/2
b = (A+(B-A)/2) - (B - (A+(B-A)/2)) = 2(A+(B-A)/2) - B = A
3) a = a + (a-b)
after this command:
a = (A+(B-A)/2) + ((A+(B-A)/2) - A) = 2(A+(B-A)/2) - A = B
b = A
for the case a>b we add another if-condition and reverse the expressions of course.
For variables supposed to represent integers we would have to do something different. I think this should work:
for the case b>a do the following
1) a = a + 2*(b-a)
2) b = b - (a-b)
3) a = a - (a-b)/2
Lets assume the initial "numeric" values for variables "a" and "b" to be "A" and "B" before the execution of these commands.
1) a = a + 2*(b-a)
after this command:
a = A + 2*(B-A) = 2*B - A
b = B
2) b = b - (a-b)
after this command:
a = 2*B - A
b = B - ((2*B-A) - B) = B - (B-A) = A
3) a = a - (a-b)/2
after this command:
a = (2*B-A) - ((2*B-A) - A)/2 = (2*B-A) - (2*B-2*A)/2 = (2*B-A) - (B-A) = B
b = A
for the case a>b we add another if-condition and reverse the expressions.
---
Now ofc two things to note are:
1) all the expression verification I did isn't really required for the methods I described(seems more suitable in the binary function case you described). Shouldn't be too difficult to see intuitively, but I can't sleep well at night without writing such clumsy algebraic expressions.
2) The second method I described would also work for variables representing rational or real numbers ofc. So it could be considered general in that sense.