Implementing xor value swap without third variable in C

Swapping variables is one of the most basic procedures that are done in programming. You only have to look at some popular algorithms like the bubble sort and the Euclides' algorithm to realize the utility of this technique. Three-variable swap 3️⃣ The first approach -and usually the most used by far- is to swap using a third variable, commonly named temporal variable. I'm sure you have seen it already. void swap(int* a, int* b) { int temp = *b *b = *a *a = temp } Yeah, that's a lot of pointers for explaining a swap. Blame C, not me

May 7, 2025 - 20:02
 0
Implementing xor value swap without third variable in C

Swapping variables is one of the most basic procedures that are done in programming.
You only have to look at some popular algorithms like the bubble sort and the Euclides' algorithm to realize the utility of this technique.

Three-variable swap 3️⃣

The first approach -and usually the most used by far- is to swap using a third variable, commonly named temporal variable. I'm sure you have seen it already.

void swap(int* a, int* b) {
    int temp = *b
    *b = *a
    *a = temp
}

Yeah, that's a lot of pointers for explaining a swap. Blame C, not me