# Swapping the values of two variables

In my previous post I have promised that I would put here some of my technical notes (JFFs). Today I am posting JFF1, which presents a very well-known problem in a non-traditional way. The problem is how to swap the values of two variables without using another temporary one.

I start by presenting the properties involved in the traditional solution and then I generalize it for arbitrary operators. I finish the note with a simple refinement. If you have any nice example instantiation for the last set of assumptions, I would be glad to know it. Other than that, comments or corrections are more than welcome!

The note is only available in PDF. Click the following link to get it:

JFF1 – Swapping the value of two variables

## 2 thoughts on “Swapping the values of two variables”

1. I’m impressed! If you find any example instantiation, be it nice or not, let us know.

P.S.
from programming.reddit.com:
“The thing is that, usually, the bitwise trick is presented using truth tables (ergh!).”
So, the truth tables push your buttons

2. sdv: truth tables correspond to case analysis, and case analysis is usually used when we can’t recognize the structure of the problem. Therefore, it should be avoided (in fact, its avoidance is one of the basic rules of the effective problem solver).

Note that using truth tables you can prove the validity of a logical formula, but if that formula contains N variables, you have to consider 2^N different cases. It is much more simple to use algebraic properties of the logical operations.