# Python Program to Find XOR of Two Numbers Without Using XOR operator

Give two numbers the task is to find the XOR result of the given two numbers in Python.

Bitwise & Operator:

If both bits are 1, sets each bit to 1.

Bitwise or (|) operator:

If one of two bits is 1, sets each bit to 1.

Examples:

Example1:

Input:

Given First Number = 4
Given Second Number = 2

Output:

The XOR result of the given first and second numbers{ 4 , 2 } = 6

Example2:

Input:

Given First Number = 7
Given Second Number = 9

Output:

The XOR result of the given first and second numbers{ 7 , 9 } = 14

## Program to Find XOR of Two Numbers Without Using XOR operator in Python

Below are the ways to find the XOR result of the given two numbers in Python:

### Method #1: Using Bitwise And(&), Or(|) Operators (Static Input)

Approach:

• Create a function XOR_result() which accepts the given two numbers as the arguments and returns the XOR result of the given first and second numbers.
• Inside the XOR_result() function.
• Calculate and the value of (gvnfst_numb | gvnscnd_numb) & (~gvnfst_numb | ~gvnscnd_numb) which gives the XOR result of the given first and second numbers and store it in a variable say xor_rslt.
• Return the value of xor_rslt (Which is the XOR result of the given first and second numbers).
• Inside the main code.
• 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.
• Pass the given first and second numbers as the arguments to XOR_result() function and store the result in a variable (xorreslt_val).
• Print the xorreslt_val value.
• The Exit of the Program.

Below is the implementation:

# Create a function XOR_result() which accepts the given two numbers as the argument and
# returns the XOR result of the given first and second numbers.

def XOR_result(gvnfst_numb, gvnscnd_numb):
# Inside the XOR_result() function.
# Calculate and the value of (gvnfst_numb | gvnscnd_numb) & (~gvnfst_numb | ~gvnscnd_numb)
# which gives the XOR result of the given first and second numbers and store it in a
# variable say xor_rslt.

xor_rslt = (gvnfst_numb | gvnscnd_numb) & (~gvnfst_numb | ~gvnscnd_numb)
# Return the value of xor_rslt (Which is the XOR result of the given first and
# second numbers).
return(xor_rslt)

# Inside the main code.
# Give the first number as static input and store it in a variable.
gvnfst_numb = 4
# Give the second number as static input and store it in another variable.
gvnscnd_numb = 2
# Pass the given first and second numbers as the arguments to XOR_result() function and
# store the result in a variable (xorreslt_val).
xorreslt_val = XOR_result(gvnfst_numb, gvnscnd_numb)
# Print the xorreslt_val value.
print("The XOR result of the given first and second numbers{",
gvnfst_numb, ",", gvnscnd_numb, "} =", xorreslt_val)


Output:

The XOR result of the given first and second numbers{ 4 , 2 } = 6

### Method #2: Using Bitwise And(&), Or(|) Operators (User Input)

Approach:

• Create a function XOR_result() which accepts the given two numbers as the argument and returns the XOR result of the given first and second numbers.
• Inside the XOR_result() function.
• Calculate and the value of (gvnfst_numb | gvnscnd_numb) & (~gvnfst_numb | ~gvnscnd_numb) which gives the XOR result of the given first and second numbers and store it in a variable say xor_rslt.
• Return the value of xor_rslt (Which is the XOR result of the given first and second numbers).
• Inside the main code.
• Give the first number as user input using the int(input()) function and store it in a variable.
• Give the second number using the int(input()) function and store it in another variable.
• Pass the given first and second numbers as the arguments to XOR_result() function and store the result in a variable (xorreslt_val).
• Print the xorreslt_val value.
• The Exit of the Program.

Below is the implementation:

# Create a function XOR_result() which accepts the given two numbers as the argument and
# returns the XOR result of the given first and second numbers.

def XOR_result(gvnfst_numb, gvnscnd_numb):
# Inside the XOR_result() function.
# Calculate and the value of (gvnfst_numb | gvnscnd_numb) & (~gvnfst_numb | ~gvnscnd_numb)
# which gives the XOR result of the given first and second numbers and store it in a
# variable say xor_rslt.

xor_rslt = (gvnfst_numb | gvnscnd_numb) & (~gvnfst_numb | ~gvnscnd_numb)
# Return the value of xor_rslt (Which is the XOR result of the given first and
# second numbers).
return(xor_rslt)

# Inside the main code.
# Give the first number as user input using the int(input()) function and store it in a variable.
gvnfst_numb = int(input('Enter some random number = '))
# Give the second number using the int(input()) function and store it in another variable.
gvnscnd_numb = int(input('Enter some random number = '))
# Pass the given first and second numbers as the arguments to XOR_result() function and
# store the result in a variable (xorreslt_val).
xorreslt_val = XOR_result(gvnfst_numb, gvnscnd_numb)
# Print the xorreslt_val value.
print("The XOR result of the given first and second numbers{",
gvnfst_numb, ",", gvnscnd_numb, "} =", xorreslt_val)


Output:

Enter some random number = 7
Enter some random number = 9
The XOR result of the given first and second numbers{ 7 , 9 } = 14