Python Program to swap two numbers without using third variable


Swapping the values of two variables is a common operation in programming. Typically, the swap is performed using a third variable to temporarily store one of the values. However, in some cases, we may want to swap two numbers without using an additional variable. This can be particularly useful in scenarios where memory optimization is a concern or when working with restricted environments.

In this article, we will explore a Python program that allows us to swap two numbers without using a third variable. We will discuss the traditional approach of using a temporary variable for swapping and introduce an alternative method that employs bitwise XOR operation. This technique provides an efficient and concise way to exchange the values of two variables without the need for an extra storage space.

Understanding the Problem

Before we delve into the solution, let's take a moment to understand the problem of swapping two numbers without using a third variable.

The traditional approach to swap two numbers involves using a third variable as a temporary storage location. The steps usually include −

  • Assign the value of the first number to the temporary variable.

  • Assign the value of the second number to the first number.

  • Assign the value of the temporary variable to the second number.

While this method is straightforward and widely used, it requires an additional variable, which may not be desirable in certain situations.

In this article, we will explore an alternative approach that leverages the bitwise XOR (exclusive OR) operation to swap the values of two numbers without using a third variable. This technique provides an elegant solution and eliminates the need for extra storage.

Next, let's understand the approach and algorithm that will allow us to achieve this swap without a third variable.

Approach and Algorithm

The approach we will use to swap two numbers without using a third variable is based on the bitwise XOR (exclusive OR) operation. XOR is a binary operation that returns 1 if the corresponding bits of the two operands are different, and 0 if they are the same.

The algorithm for swapping two numbers without a third variable using XOR is as follows −

  • Take two numbers, let's call them a and b.

  • XOR a with b and store the result back in a. This operation effectively merges the bits of a and b without losing any information.

  • XOR the updated value of a with b and store the result back in b. This XOR operation will cancel out the common bits between a and b, leaving only the original value of a in b.

  • XOR the updated value of a with b and store the result back in a. This final XOR operation will cancel out the original value of a in b, leaving only the original value of b in a.

By performing these XOR operations, the values of a and b will be swapped without the need for a third variable.

This approach works because XOR is a bitwise operation that manipulates individual bits. It allows us to perform the swap operation in-place, directly modifying the memory locations of a and b.

Example

Now that we have discussed the approach and algorithm, let's see how we can implement the swapping of two numbers without using a third variable in Python.

# Swapping two numbers without using a third variable
def swap_numbers(a, b):
    print("Before swapping: a =", a, "b =", b)
  
    # Performing the XOR operations to swap the values
    a = a ^ b
    b = a ^ b
    a = a ^ b
  
    print("After swapping: a =", a, "b =", b)

# Testing the function
a = 10
b = 5
swap_numbers(a, b)

The code starts by defining the swap_numbers function that takes two parameters a and b. Inside the function, we first print the values of a and b before the swapping operation using the print function and string formatting.

Next, we perform the swapping of a and b using the XOR operations. The XOR operation a = a ^ b combines the bits of a and b without losing any information. Then, the XOR operation b = a ^ b cancels out the common bits between a and b, effectively storing the original value of a in b. Finally, the XOR operation a = a ^ b cancels out the original value of a in b, leaving only the original value of b in a.

After performing the swapping operations, we print the updated values of a and b using the print function and string formatting.

By utilizing the XOR operations, the code effectively swaps the values of a and b without using a third variable. This approach allows for an efficient and concise swapping mechanism.

The swap numbers function is then called with the initial values of a and b to test the swapping functionality. The output of the function call is displayed, showcasing the values of a and b before and after the swapping operation.

Output

When you run the code, you will see the output displaying the values of a and b before and after the swapping operation.

Before swapping: a = 10 b = 5
After swapping: a = 5 b = 10

Conclusion

In this article, we have explored a Python program to swap two numbers without using a third variable. We discussed the concept of XOR operations and how they can be leveraged to perform the swapping efficiently.

Updated on: 10-Aug-2023

348 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements