Bitwise xor python – Python Program to Swap Two Numbers using Bitwise Operators

Bitwise xor python: 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 or Swapping Of Two Numbers Using Bitwise Operator In Python, Swapping Using Xor In Python, Swap 2 Numbers Using Bitwise Operator, Swapping Of Two Numbers In Python, Bitwise Xor In Python, Swap Two Numbers Using Bitwise Operator In C, Xor Operation In Python, Swapping Two Numbers Using Bitwise Operator In C, C Program To Swap Two Numbers Using Bitwise Operator, Python Bit Operations, Bitwise Xor Python.

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

Python xor operator: 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.

Recommended Reading On: Python Program to Multiply Two Numbers Without Using Multiplication(*) Operator

Applications On: 

  1. Write A Program To Swap Two Numbers Using Bitwise Operators In Python
  2. Write A Program To Swap Two Numbers Using Bitwise Operators In C
  3. What Is Bitwise Operator In Python
  4. How To Swap Two Numbers In Python
  5. Write A Program To Swap Two Numbers In Python
  6. Write A Program To Swap Two Numbers Using Bitwise Operators

Related Posts On: