Nội dung chính
- More Examples
- Bitwise Operator
- Python XOR Operator
- XOR using Operator Module
- Sign Up for Our Newsletters
- How do I get XOR 2 values?
- How is XOR calculated in Python?
- What is the XOR of two numbers?
- What is the shortcut to find XOR of two numbers?
Python bitwise operators are used to perform bitwise calculations on integers. First, the integers are converted into binary format, and then operations are performed bit by bit, hence the name the bitwise operators.
Python bitwise operators work on integers only, and the final output is returned in the decimal format. Python bitwise operators are also called binary operators.
XOR in Python is also known as “exclusive or” that compares two binary numbers bitwise. If both bits are the same, the XOR operator outputs 0. If both bits are different, the XOR operator outputs 1. The Bitwise XOR sets the input bits to 1 if either, but not both, of the analogous bits in the two operands is 1.
Use the XOR operator ^ between two values to perform bitwise “exclusive or” on their binary representations.
For example, when used between two integers, the XOR operator returns an integer.
Example
output = 19 ^ 21 print[output]
Output
6
We have used the XOR operator between two integers. When used between two integers, the XOR operator returns an integer.
When performing XOR on two booleans, True is treated as 1, and False is treated as 0. Thus, XOR between two booleans returns a boolean.
result = True ^ False print[result]
Output
True
Let’s compare two False values.
result = False ^ False print[result]
Output
False
Let’s compare two True values.
result = True ^ True print[result]
Output
False
From the above code example, you can see that if two True or False values are compared, it returns False, but if two different values are compared, it will return True.
More Examples
See the following code.
result = bin[0b1111 ^ 0b1111] print[result]
Output
0b0
Let’s see how to swap integers without a temporary variable using XOR.
a = 21 b = 19 print['The value of a is: ', a] print['The value of b is: ', b] a ^= b b ^= a a ^= b print['After swapping: '] print['The value of a is: ', a] print['The value of b is: ', b]
Output
The value of a is: 21 The value of b is: 19 After swapping: The value of a is: 19 The value of b is: 21
That’s it for this tutorial.
See also
Python Division
Python Square
Python sftp
Python Modulo
Python rstrip
XOR Operator in Python is also known as “exclusive or” that compares two binary numbers bitwise if two bits are identical XOR outputs as 0 and when two bits are different then XOR outputs as 1. XOR can even be used on booleans.
XOR is mainly used in situations where we don’t want two conditions to be true simultaneously. In this tutorial, we will look explore multiple ways to perform XOR [exclusive OR] operations in Python with examples.
Bitwise Operator
Bitwise operators in Python are also called binary operators, and it is mainly used to perform Bitwise calculations on integers, the integers are first converted into binary, and later the operations are performed bit by bit.
Python XOR Operator
Let’s take a look at using the XOR ^
Operator between 2 integers. When we perform
XOR between 2 integers, the operator returns the integer as output.
a= 5 #0101
b = 3 #0011
result = [a ^ b] #0110
print[result]
# Output
# 6 [0110]
Let’s take a look at using XOR on two booleans. In the case of boolean, the true is treated as 1, and the false is treated as 0. Thus the output returned will be either true or false.
print[True ^ True]
print[True ^ False]
print[False ^ True]
print[False ^ False]
Output
False
True
True
False
XOR using Operator Module
We can even achieve XOR using the built-in operator
module in Python. The operator module has a
xor[]
function, which can perform an XOR operation on integers and booleans, as shown below.
import operator
print[operator.xor[5,3]]
print[operator.xor[True,True]]
print[operator.xor[True,False]]
print[operator.xor[False,True]]
print[operator.xor[False,False]]
Output
6
False
True
True
False
- Bitwise Operator,
- exclusive or,
- operator,
- XOR
Sign Up for Our Newsletters
Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.
By checking this box, you confirm that you have read and are agreeing to our terms of use regarding the storage of the data submitted through this form.
How do I get XOR 2 values?
To find the XOR of two numbers, follow these instructions:.
Convert the numbers into the binary representation..
Compare the corresponding bits of the two numbers..
If only one of the input bits is true [1], the output is true [1]. Otherwise, the output is false [0]..
How is XOR calculated in Python?
In Python, bitwise operators are used to performing bitwise calculations on integers. The integers are first converted into binary and then operations are performed on bit by bit, hence the name bitwise operators. ... Bitwise operators..
What is the XOR of two numbers?
XOR is defined as exclusive or for two integers say a and b. To find XOR, we will first find the binary representation of both a and b. Lets do this also by example. Suppose a = 7 and b = 10.
What is the shortcut to find XOR of two numbers?
The XOR works by setting the bits which are set in either of one of the given numbers [0 ^ 1 = 1, 1 ^ 0 = 1] and finally taking out the common bits present in both numbers [1 ^ 1 = 0] . Now, the result x ^ y would be [x | y] - [x & y] = [01010001 - 01000000] = 00010001 .