- Python XOR: How to Use Bitwise XOR Operator
- Example 1: How to Use XOR in Python
- Performing XOR on two booleans
- Example 3: Swapping two integers using XOR without a temporary variable
- Example 4: Using XOR with bin() function
- What is the difference between XOR and OR?
- What is XOR in Python?
- How to perform the bitwise XOR in Python?
- XOR ^ Operator between 2 integers
- Performing XOR on two booleans
- Swapping two integers using XOR without a temporary variable
- XOR in Python using Operator Module
- Learn more about bitwise operators
- Conclusion
- XOR in Python
- Bitwise Operator
- Python XOR Operator
- XOR using Operator Module
- how to do bitwise exclusive or of two strings in python?
- 12 Answers 12
Python XOR: How to Use Bitwise XOR Operator
XOR sets the input bits to 1 if either, but not both, of the analogous bits in the two operands is 1.
Example 1: How to Use XOR in Python
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.
output = 19 ^ 21 print(output)
We have used the XOR operator between two integers. When used between two integers, the XOR operator returns an integer.
Performing XOR on two booleans
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)
Let’s compare two False values.
result = False ^ False print(result)
Let’s compare two True values.
result = True ^ True print(result)
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.
Example 3: Swapping two integers using XOR without a temporary variable
In Python, you can use the XOR (^) operator to swap two integers without using a temporary variable. The XOR operator returns a number that is the result of a binary “exclusive or” operation on the operands.
The “exclusive or” operation returns True if exactly one of the operands is True. If both operands are True or both are False, it returns False.
x = 5 y = 10 print('Before swapping: x =', x, ', y =', y) x = x ^ y # Step 1 y = x ^ y # Step 2 x = x ^ y # Step 3 print('After swapping: x =', x, ', y =', y)
Before swapping: x = 5 , y = 10 After swapping: x = 10 , y = 5
Example 4: Using XOR with bin() function
result = bin(0b1111 ^ 0b1111) print(result)
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)
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
What is the difference between XOR and OR?
The main difference between XOR and OR operators is that XOR returns True if precisely one of the operands is True, whereas OR returns True if at least one of the operands is true.
The second difference is that XOR is the exclusive operator, and OR is an inclusive operator.
What is XOR in Python?
In Python, XOR is a bitwise operator that is also known as Exclusive OR.
It is a logical operator which outputs 1 1 1 when either of the operands is 1 1 1 (one is 1 1 1 and the other one is 0 0 0 ), but both are not 1 1 1 , and both are not 0 0 0 .
The symbol for XOR in Python is ‘^’ and in mathematics, its symbol is ‘⊕’.
The XOR operator is placed between two numbers or boolean values.
How to perform the bitwise XOR in Python?
In Python, we can perform the bitwise XOR operation using the «^» symbol. The XOR operation can be used for different purposes; XOR of two integers, XOR of two booleans, Swapping two numbers using XOR, etc.
We can also use the xor() function using the operator module in Python.
XOR ^ Operator between 2 integers
As XOR is a bitwise operator, it will compare bits of both the integers bit by bit after converting them into binary numbers.
Truth table for XOR (binary)
In the above example, we are finding the XOR of 1 5 15 1 5 and 3 2 32 3 2 , the result of which is 4 7 47 4 7 . The XOR operator first converted both the numbers in their binary form and then compared their bits bitwise.
To understand the working of XOR operation better, let us find the XOR of 15 and 32 by comparing their bits:
1 5 = 0 b 0 0 1 1 1 1 15 = 0b001111 1 5 = 0 b 0 0 1 1 1 1
3 2 = 0 b 1 0 0 0 0 0 32 = 0b100000 3 2 = 0 b 1 0 0 0 0 0
⟹ 1 5 ⊕ 3 2 = 0 b 0 0 1 1 1 1 ⊕ 0 b 1 0 0 0 0 0 \implies15 ⊕ 32 = 0b001111 ⊕ 0b100000 ⟹ 1 5 ⊕ 3 2 = 0 b 0 0 1 1 1 1 ⊕ 0 b 1 0 0 0 0 0
⟹ 1 5 ⊕ 3 2 = 0 b 1 0 1 1 1 1 \implies15 ⊕ 32 = 0b101111 ⟹ 1 5 ⊕ 3 2 = 0 b 1 0 1 1 1 1
The XOR of 1 5 15 1 5 and 3 2 32 3 2 is 0 b 1 0 1 1 1 1 0b101111 0 b 1 0 1 1 1 1 , i.e., 4 7 47 4 7 .
Performing XOR on two booleans
XOR results T r u e True T r u e when either of the operands are T r u e True T r u e (one is T r u e True T r u e and the other one is F a l s e False F a l s e ) but both are not T r u e True T r u e and both are not F a l s e False F a l s e .
Truth table for XOR (boolean)
In the above example, we are finding the XOR of the boolean values ( T r u e True T r u e and F a l s e False F a l s e ).
Swapping two integers using XOR without a temporary variable
The XOR swap algorithm can swap the values of two integers without the use of a temporary variable, which is normally required in other swapping algorithms.
In the above program, we are swapping two integers without using a temporary variable with the help of the XOR operator .
XOR in Python using Operator Module
We can also use the XOR operation using the xor() function in the operator module. The xor() function can perform XOR operations on integers and booleans.
The above example uses the operator.xor() function with booleans and integers.
Learn more about bitwise operators
The bitwise operators are used to perform bitwise calculations on integers. The integers are first converted into binary numbers, and then the operations are performed bit by bit. The result is always in decimal format.
They can also be used with boolean values.
To learn more about bitwise operators, click here.
Conclusion
- XOR is a bitwise operator that is short for Exclusive OR.
- It returns 1 1 1 when one and only one of the operands are 1 1 1 or T r u e True T r u e .
- XOR in Python is denoted using the «^» symbol.
- We can swap two integers without using a temporary variable with the help of the XOR operation.
- XOR operation can also be used with the help of the xor() function by importing the operator module.
XOR in Python
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)
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))
how to do bitwise exclusive or of two strings in python?
I would like to perform a bitwise exclusive or of two strings in python, but xor of strings are not allowed in python. How can I do it ?
12 Answers 12
You can convert the characters to integers and xor those instead:
l = [ord(a) ^ ord(b) for a,b in zip(s1,s2)]
Here’s an updated function in case you need a string as a result of the XOR:
def sxor(s1,s2): # convert strings to a list of character pair tuples # go through each tuple, converting them to ASCII code (ord) # perform exclusive or on the ASCII code # then convert the result back to ASCII (chr) # merge the resulting array of characters as a string return ''.join(chr(ord(a) ^ ord(b)) for a,b in zip(s1,s2))
I disagree. If doing cryptographic or other similar data manipulation operations in Python you want to be able to do this on strings of bytes. In my opinion Python3 should support this operation on byte strings.
or something like bytes(x ^ y for x, y in zip(s1, s2)) in python3 as you may see below 🙂 stackoverflow.com/questions/2612720/…
If you want to operate on bytes or words then you’ll be better to use Python’s array type instead of a string. If you are working with fixed length blocks then you may be able to use H or L format to operate on words rather than bytes, but I just used ‘B’ for this example:
>>> import array >>> a1 = array.array('B', 'Hello, World!') >>> a1 array('B', [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]) >>> a2 = array.array('B', ('secret'*3)) >>> for i in range(len(a1)): a1[i] ^= a2[i] >>> a1.tostring() ';\x00\x0f\x1e\nXS2\x0c\x00\t\x10R'
For bytearrays you can directly use XOR:
>>> b1 = bytearray("test123") >>> b2 = bytearray("321test") >>> b = bytearray(len(b1)) >>> for i in range(len(b1)): . b[i] = b1[i] ^ b2[i] >>> b bytearray(b'GWB\x00TAG')
Here is your string XOR’er, presumably for some mild form of encryption:
>>> src = "Hello, World!" >>> code = "secret" >>> xorWord = lambda ss,cc: ''.join(chr(ord(s)^ord(c)) for s,c in zip(ss,cc*100)) >>> encrypt = xorWord(src, code) >>> encrypt ';\x00\x0f\x1e\nXS2\x0c\x00\t\x10R' >>> decrypt = xorWord(encrypt,code) >>> print decrypt Hello, World!
Note that this is an extremely weak form of encryption. Watch what happens when given a blank string to encode:
>>> codebreak = xorWord(" ", code) >>> print codebreak SECRET