Program to Swap Two Numbers using Bitwise Operators

Python Program to Swap Two Numbers using Bitwise Operators

In the previous article, we have discussed Python Program to Check Even or Odd Using Bitwise Operator

Given two numbers and the task is to swap the given two numbers using Bitwise Operators.

Bitwise XOR operation:

Sets each bit to 1 if only one of two bits is 1

Examples:

Example1:

Input:

Given First Number= 35
Given Second Number = 20

Output:

The Result After Swapping  given two Numbers is:
First Number =  20
Second Number =  35

Example2:

Input:

Given First Number= 54
Given Second Number = 10

Output:

The Result After Swapping given two Numbers is:
First Number = 10 
Second Number = 54

Program to Swap Two Numbers using Bitwise Operators in Python

Below are the ways to swap the given two numbers using Bitwise Operators in Python:

Method #1: Using Bitwise XOR Operator (Static Input)

Approach:

  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Apply bitwise ^ operator on the first and second numbers and store it in the first number.
  • Again apply the bitwise ^ operator on the first and second numbers and store it in the second number.
  • Apply bitwise ^ operator on the first and second numbers and store it in the first number.
  • Print the given numbers after swapping.
  • The Exit of the Program.

Below is the implementation:

# Give the first number as static input and store it in a variable.
fst_numb = 54
# Give the second number as static input and store it in another variable.
scnd_numb = 10
# Apply bitwise ^ operator on the first and second numbers and store it in the
# first number.
fst_numb = fst_numb ^ scnd_numb
# Again apply the bitwise ^ operator on the first and second numbers and store it in the
# second number.
scnd_numb = fst_numb ^ scnd_numb
# Apply bitwise ^ operator on the first and second numbers and store it in the
# first number.
fst_numb = fst_numb ^ scnd_numb
# Print the given numbers after swapping.
print("The Result After Swapping  given two Numbers is:")
print("First Number = ", fst_numb)
print("Second Number = ", scnd_numb)

Output:

The Result After Swapping  given two Numbers is:
First Number =  10
Second Number =  54

Method #2: Using Bitwise XOR Operator (User Input)

Approach:

  • Give the first number as user input using the int(input()) function and store it in a variable.
  • Give the second number as user input using the int(input()) function and store it in another variable.
  • Apply bitwise ^ operator on the first and second numbers and store it in the first number.
  • Again apply the bitwise ^ operator on the first and second numbers and store it in the second number.
  • Apply bitwise ^ operator on the first and second numbers and store it in the first number.
  • Print the given numbers after swapping.
  • The Exit of the Program.

Below is the implementation:

# Give the first number as user input using the int(input()) function and
# store it in a variable.
fst_numb = int(input("Enter some random number = "))
# Give the second number as user input using the int(input()) function and
# store it in another variable.
scnd_numb = int(input("Enter some random number = "))
# Apply bitwise ^ operator on the first and second numbers and store it in the
# first number.
fst_numb = fst_numb ^ scnd_numb
# Again apply the bitwise ^ operator on the first and second numbers and store it in the
# second number.
scnd_numb = fst_numb ^ scnd_numb
# Apply bitwise ^ operator on the first and second numbers and store it in the
# first number.
fst_numb = fst_numb ^ scnd_numb
# Print the given numbers after swapping.
print("The Result After Swapping  given two Numbers is:")
print("First Number = ", fst_numb)
print("Second Number = ", scnd_numb)

Output:

Enter some random number = 45
Enter some random number = 65
The Result After Swapping given two Numbers is:
First Number = 65
Second Number = 45

If you are learning Python then the Python Programming Example is for you and gives you a thorough description of concepts for beginners, experienced programmers.