<p>One solution is using XOR as following:</p>
<p><span class="math-container">\begin{eqnarray}
*x = (*x)\wedge(*y);\tag{1}\\
*y = (*x)\wedge(*y);\tag{2}\\
*x = (*x)\wedge(*y)\tag{3};
\end{eqnarray}</span></p>

CC BY-SA 3.0
2012-02-10T09:06:53.483Z
Please don't answer such questions. Doing so invites more questions which are not suitable for this forum.
Andrej Bauer
CC BY-SA 3.0
2012-02-10T10:55:05.073Z
Knowing how to do this while knowing not much else about programming can be harmful. Many years ago when I was a 2nd year student coding for a huge project as a summer job, I made a C macro for swapping based on the above. Since swap dominated the computation, there was a non-negligible blowout in code size after macro expansion and performance penalty, puzzling the senior members of the team – until they saw my macro.
Kai
CC BY-SA 3.0
2012-02-10T13:27:43.793Z
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).
David Harris
CC BY-SA 3.0
2012-02-10T13:28:44.557Z
Also, note that when $x = y$ this command sets \*x = \*y = 0. You *need* to check if x = y as a special case.
David Harris
CC BY-SA 3.0
2012-02-10T14:13:48.417Z
@DavidHarris You are right, nevertheless it can be fixed: *x = *x + *y; *y = *x - *y; *x = *x - *y; (for ints only).
Dmytro Korduban
CC BY-SA 3.0
2012-02-10T14:19:54.563Z
@maldini.ua That still suffers from aliasing - you will end up setting *x and *y to 0
Klaus Draeger
CC BY-SA 3.0
2012-02-10T14:28:20.987Z
@KlausDraeger Oops, disregard my comment. I had something different in my mind. =(
Dmytro Korduban
CC BY-SA 4.0
2012-02-10T05:27:38.520Z
2021-03-05T15:40:14.507Z
2021-03-05T15:40:14.507Z
mghandi
<p>how to swap the value of two variables without an auxilary (helping) variable ? </p>

<p>swap(int *x,int *y)
{
  int h = *x; 
  *x = *y; 
  *y = h; 
}</p>

<p>how can we do it without introducing h?</p>

CC BY-SA 3.0
2012-02-10T05:40:41.170Z
This is not a research-level question.
Michael Blondin
<ds.algorithms>
how to swap the value of two variables without an auxilary (helping) variable
Austin
# how to swap the value of two variables without an auxilary (helping) variable

(Asked by: username_0 on 2012-02-10)

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?

**Tags:** ds.algorithms
> **Comments:**
> **username_1**: This is not a research-level question.  

---

## 1 answers

### Answer by username_2 on 2012-02-10. Score: 1 | Accepted answer

One solution is using XOR as following:

\begin{eqnarray}
\*x = (\*x)\wedge(\*y);\tag{1}\\
\*y = (\*x)\wedge(\*y);\tag{2}\\
\*x = (\*x)\wedge(\*y)\tag{3};
\end{eqnarray}

> **Comments:**
> **username_3**: Please don't answer such questions. Doing so invites more questions which are not suitable for this forum.  
> **username_4**: Knowing how to do this while knowing not much else about programming can be harmful. Many years ago when I was a 2nd year student coding for a huge project as a summer job, I made a C macro for swapping based on the above. Since swap dominated the computation, there was a non-negligible blowout in code size after macro expansion and performance penalty, puzzling the senior members of the team – until they saw my macro.  
> **username_5**: 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).  
> **username_5**: Also, note that when $x = y$ this command sets \*x = \*y = 0. You *need* to check if x = y as a special case.  
> **username_6**: @DavidHarris You are right, nevertheless it can be fixed: *x = *x + *y; *y = *x - *y; *x = *x - *y; (for ints only).  
> **username_7**: @maldini.ua That still suffers from aliasing - you will end up setting *x and *y to 0  
> **username_6**: @KlausDraeger Oops, disregard my comment. I had something different in my mind. =(  

---

