swap integers
algorithm
programming tricks
coding techniques
variable manipulation

Swap two integers without using a third variable

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Swapping two integers without using a third variable is a classic problem in computer science and programming that exemplifies clever use of arithmetic or bitwise operations. The traditional swapping method using a temporary variable is simple but not as instructive when it comes to understanding underlying system operations. By avoiding a temporary variable, we make the code slightly more efficient and learn about data manipulation at a deeper level.

Techniques for Swapping Without a Third Variable

There are primarily two techniques to swap integers without a third variable: arithmetic operations and bitwise XOR operations. Let's explore both methods in detail.

1. Arithmetic Operations

This technique involves using basic arithmetic operators to swap the values of two integers. The key operations used here are addition and subtraction.

Algorithm

Assume you have two integers `a` and `b`. Here is the step-by-step algorithm:

  1. Addition Assignment:
    • Assign `a = a + b`. Now, the value of `a` is the sum of `a` and `b`.
  2. Subtraction for `b`:
    • Assign `b = a - b`. This effectively results in `b` becoming the original value of `a`.
  3. Subtraction for `a`:
    • Assign `a = a - b`. This results in `a` taking the original value of `b`.

Example

Consider `a = 5` and `b = 3`:

  1. `a = a + b` → `a = 5 + 3 = 8`
  2. `b = a - b` → `b = 8 - 3 = 5`
  3. `a = a - b` → `a = 8 - 5 = 3`

Now, `a = 3` and `b = 5`.

2. Bitwise XOR Operation

The XOR operation is a bitwise operation that can be effectively used to swap values. It doesn't involve risky overflow situations that can occur with very large integers in the arithmetic method.

Algorithm

Given two integers `a` and `b`, use the following steps:

  1. First XOR operation:
    • Assign `a = a XOR b`. This stores the XOR result between `a` and `b` in `a`.
  2. Second XOR operation:
    • Assign `b = a XOR b`. This makes `b` store the original value of `a`.
  3. Third XOR operation:
    • Assign `a = a XOR b`. This makes `a` store the original value of `b`.

Example

Consider `a = 5` (`0101` in binary) and `b = 3` (`0011` in binary):

  1. `a = a XOR b` → `a = 0101 XOR 0011 = 0110` (`a = 6`)
  2. `b = a XOR b` → `b = 0110 XOR 0011 = 0101` (`b = 5`)
  3. `a = a XOR b` → `a = 0110 XOR 0101 = 0011` (`a = 3`)

Finally, `a = 3` and `b = 5`.

Summary Table

Below is a table summarizing the methods used for swapping integers without a third variable:

MethodSteps & Example (a = 5, b = 3)AdvantagesDisadvantages
Arithmetica = a + b b = a - b a = a - b- Simple to understand- May cause overflow with large values
Bitwise XORa = a XOR b b = a XOR b a = a XOR b- No overflow issues - Efficient for bitwise operations- Less intuitive for beginners

Subtopics

Application in Programming

Swapping values without a temporary variable may not have significant efficiency gains in most modern applications, but understanding these methods enhances comprehension of bitwise and arithmetic operations. These techniques are sometimes used in systems where low-level programming and hardware optimizations are crucial.

Edge Cases and Considerations

  • Overflow in Arithmetic: While using arithmetic, the addition operation can result in overflow when dealing with systems with fixed integer size, such as `int` in C/C++.
  • Negative Numbers: Both methods effectively handle negative numbers.

Conclusion

Swapping integers without a third variable is a great way to delve into the basics of computer arithmetic and bitwise operations. It fosters a deeper understanding of how computers handle data and the internal workings of operations, providing foundational knowledge that can be utilized in optimization and algorithm development. Whether using arithmetic or bitwise operations, these methods showcase the elegance of simple yet clever programming strategies.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.