Python Bitwise XOR Operator
Binary language is the language of a computer. All the inner mechanisms of a computer happen concerning bits. Bitwise operators are the set of operators that allow the programmer to perform bitwise operations on integers. These operators allow the programmer to manipulate the lower-level data in a computer. There are a total of six bitwise operators in Python:
This article discusses the Bitwise XOR operator with examples.
Now, let us understand the operation of Bitwise XOR:
The truth table of XOR:
Let us understand the concept using an example:
If we want to perform:
3 ^ 4:
1. Both the integers are converted into binary forms:
3 -> 0 1 1
4 -> 1 0 0
2. Now, xor is applied bitwise:
3. Finally, the resultant binary number is converted back to its decimal form.
1 1 1 represents 7
4. Output -> 7
3 ^ 4 -> 7
Now, for the same operands, let us write a code for xor:
On Boolean values:
We can perform xor on Boolean values. When we operate on two integers, 1 is equivalent to Boolean True, and 0 is equivalent to Boolean False.
Structure of outputs:
Let us take the above example:
If a ^ b -> c
c ^ a -> b
c ^ b -> a
Role of xor in cryptography:
The above relation got xor a small role in cryptography models.
If A wants to send the message 3 to B and their secret key is 4
3 ^ 4 -> 5.
A will send 5 to B.
B already knows the secret key is 4.
He will decrypt the message using xor again:
5 ^ 4 -> 3
Message successfully received.
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected]
Duration: 1 week to 2 week