# how to swap the value of two variables without an auxilary (helping) variable [closed]

how to swap the value of two variables without an auxilary (helping) variable ?

swap(int *x,int *y) { int h = *x; *x = *y; *y = h; }

how can we do it without introducing h?

• This is not a research-level question. Feb 10 '12 at 5:40

$$\begin{eqnarray} *x = (*x)\wedge(*y);\tag{1}\\ *y = (*x)\wedge(*y);\tag{2}\\ *x = (*x)\wedge(*y)\tag{3}; \end{eqnarray}$$
• The problem with this solution is that it is not type-safe. This solution implicitly assumes that there is some encoding of $x, y$ as bits. As far as I know, there is no general type-safe method of swapping two values. (Although some programming languages contain a special command to swap elements). Feb 10 '12 at 13:27
• Also, note that when $x = y$ this command sets *x = *y = 0. You need to check if x = y as a special case. Feb 10 '12 at 13:28