0
0
CProgramBeginner · 2 min read

C Program to Swap Two Numbers Using XOR Operator

You can swap two numbers in C without a temporary variable using XOR operator like this: a = a ^ b; b = a ^ b; a = a ^ b; which swaps values of a and b.
📋

Examples

Inputa=5, b=10
OutputAfter swap: a=10, b=5
Inputa=0, b=0
OutputAfter swap: a=0, b=0
Inputa=-3, b=7
OutputAfter swap: a=7, b=-3
🧠

How to Think About It

To swap two numbers using XOR, think of XOR as a way to combine bits without losing information. By applying XOR three times between the two numbers, you can exchange their values without needing extra space for a temporary variable.
📐

Algorithm

1
Get input values for two numbers a and b
2
Apply XOR between a and b and store in a
3
Apply XOR between new a and b and store in b
4
Apply XOR between new a and new b and store in a
5
Print the swapped values of a and b
💻

Code

c
#include <stdio.h>

int main() {
    int a = 5, b = 10;
    printf("Before swap: a=%d, b=%d\n", a, b);
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
    printf("After swap: a=%d, b=%d\n", a, b);
    return 0;
}
Output
Before swap: a=5, b=10 After swap: a=10, b=5
🔍

Dry Run

Let's trace swapping a=5 and b=10 through the XOR swap code

1

Initial values

a=5 (0101), b=10 (1010)

2

a = a ^ b

a = 0101 ^ 1010 = 1111 (15), b=10

3

b = a ^ b

b = 1111 ^ 1010 = 0101 (5), a=15

4

a = a ^ b

a = 1111 ^ 0101 = 1010 (10), b=5

Stepa (decimal)b (decimal)
Initial510
After a = a ^ b1510
After b = a ^ b155
After a = a ^ b105
💡

Why This Works

Step 1: First XOR operation

When you do a = a ^ b, you store combined bits of both numbers in a without losing information.

Step 2: Second XOR operation

Then b = a ^ b extracts the original value of a and assigns it to b.

Step 3: Third XOR operation

Finally, a = a ^ b extracts the original value of b and assigns it to a, completing the swap.

🔄

Alternative Approaches

Using a temporary variable
c
#include <stdio.h>

int main() {
    int a = 5, b = 10, temp;
    printf("Before swap: a=%d, b=%d\n", a, b);
    temp = a;
    a = b;
    b = temp;
    printf("After swap: a=%d, b=%d\n", a, b);
    return 0;
}
This method is simple and clear but uses extra memory for a temporary variable.
Using addition and subtraction
c
#include <stdio.h>

int main() {
    int a = 5, b = 10;
    printf("Before swap: a=%d, b=%d\n", a, b);
    a = a + b;
    b = a - b;
    a = a - b;
    printf("After swap: a=%d, b=%d\n", a, b);
    return 0;
}
This method avoids extra variables but can cause overflow if numbers are large.

Complexity: O(1) time, O(1) space

Time Complexity

The swap uses a fixed number of operations (3 XORs), so it runs in constant time.

Space Complexity

No extra memory is used besides the input variables, so space complexity is constant.

Which Approach is Fastest?

All methods run in constant time; XOR swap uses no extra memory but is less readable than using a temporary variable.

ApproachTimeSpaceBest For
XOR SwapO(1)O(1)Memory-constrained environments
Temporary VariableO(1)O(1)Readability and safety
Addition/SubtractionO(1)O(1)Avoiding extra variables but watch overflow
💡
Use XOR swap to save memory but prefer temporary variable swap for clarity and safety.
⚠️
Forgetting that XOR swap fails if both variables point to the same memory location.