# XOR in Python: How to Use Bitwise XOR Operator

Operators are the building block of any expression that we see. Be it mathematical or logical, without operators it would become very difficult to explain what operations we want to perform.

Imagine having to drive a car that does not have a steering wheel, it would be impossible to tell where the car is going to go. Similarly, without operators, we would not be able to make the compiler or interpreter of our code understand what operations we are performing. One such operator we shall see today is the XOR operator.

## What is the XOR operator?

Also known as the exclusive OR, the XOR operator is a bitwise operator. What are bitwise operators? Any operator that operates on binary numbers is called a bitwise operator. They are used to perform some special operation on a binary string, that is a sequence of 0s and 1s.

Binary operators are used to performing operations on the bit level, which affects every bit in the number individually.

So, What is an exclusive OR operator? The XOR operation returns a 1 if the two operand bits are different and it returns 0 if the two operand bits are the same.

Let us simplify the technical jargon a bit! Take two binary numbers, 1100 and 0010. '⊕' is the symbol for the exclusive OR operation and the XOR operation between the two binary numbers can be written as:

1100 ⊕ 0010

the result of this will be 1110.

How did we get that result? Let us see the results bit by bit, literally speaking, have a look at the following table to understand how the results came.

 A B Result 1 0 1 1 0 1 0 1 1 0 0 0

What do we notice? When the two bits are different, the resulting bit is always 1 and when the two bits are the same, the resulting bit is always 0. That is the rule for Exclusive OR. Here is an image for easy reference: Now that we know how it works mathematically, let's see its implementation in python.

## How to find XOR value in python?

The XOR operator in python can be applied using the “^” symbol. The XOR operator is used in various situations that we shall see in the section below, for now, let us have a look at how the code is implemented:

```A = 0b1100
B = 0b0010

print(bin(A^B))```

Output:

```0b1110
```

## Use cases of XOR

### 01) Encryption

Exclusive OR is one of the first methods that was used in encryption. It is a very naive method but it is very easy to implement. Though using exclusive OR alone is a very weak method of implementing encryption, combined with other methods, it can create powerful encryption.

Let us have a look at an example of how encryption using XOR or exclusive OR would look. We have a data item '10' that we want to send across to our friend. First, we decide on a key, which is a number that only we and our friends will know. Let us say this key is 5. The next step is using this key for encrypting the number.

We perform the XOR operation on 10 and 5. The interpreter will first convert the decimal numbers to binary numbers and then perform the operation. Once the resulting binary number is found, it converts it back to a decimal number again. So,

10 ⊕ 5 = 1010 ⊕ 0101 = 1111 = 15

After getting the result 15 we send the encrypted message to our friend. Our friend receives the encrypted message 15 and needs to extract the actual message. They use the common key we had decided first, they perform the XOR operation on 15 and 5.

15 ⊕ 5 = 1111 ⊕ 0101 = 1010 = 10

Now our friend has the original message.

### 02) Swapping Two Numbers

We just saw how the XOR operation is used for encryption, let us now see how it can be used to perform the swap between two numbers. Let us say we have two numbers 10 and 15 that we wish to swap. 10 is stored in variable A and 15 is stored in variable B. We perform the swaps using the XOR operation in the following manner:

A ⊕ B = 10 ⊕ 15 = 1010 ⊕ 1111 = 0101 = 5

We store the result in A, so A becomes 0101. Now, we do A ⊕ B again,

A ⊕ B = 5 ⊕ 15 = 0101 ⊕ 1111 = 1010 = 10

We store this result in B, so B becomes 1010. Then we perform A ⊕ B again,

A ⊕ B = 0101 ⊕ 1010 = 1111 = 15

We store this result in A. Therefore now the values of A and B are 15 and 10. The swap has taken place. What actions did we perform?

1. A = A ⊕ B
2. B = A ⊕ B
3. A = A ⊕ B

Here is the source code for implementation:

```A = 10
B = 15
print("Before:",A,B)
A = A^B
B = A^B
A = A^B
print("After",A,B)```

Output:

```Before: 10 15
After 15 10
```

### 03) Checking equality

How do we use XOR for checking equality? Do we know the fundamental rule about XOR? It returns 1 if the two operands are different and 0 if the two operands are the same.

Let us say we had to compare two boolean values. We can XOR the two boolean values and if it returns 1 we can say they are not equal and if it returns 0 we can say they are equal.

## Conclusion

We saw one of the bitwise operators, the XOR or Exclusive OR operator. Bitwise operators work on the given operands one bit by one bit. The XOR operation is also used for various tasks like encryption, swapping, and equality checks.

### FavTutor - 24x7 Live Coding Help from Expert Tutors!
{"ft_lang_label":"__label__en","ft_lang_prob":0.92903906,"math_prob":0.98997444,"size":4945,"snap":"2023-40-2023-50","text_gpt3_token_len":1279,"char_repetition_ratio":0.14329083,"word_repetition_ratio":0.034764826,"special_character_ratio":0.27563196,"punctuation_ratio":0.096466094,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99758905,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,1,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-28T09:03:09Z\",\"WARC-Record-ID\":\"<urn:uuid:704cefb0-79ae-4c03-926c-bcfed5c2c02c>\",\"Content-Length\":\"48180\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:890a5e1d-6dff-47d4-9c47-d30ddee08d98>\",\"WARC-Concurrent-To\":\"<urn:uuid:069a7c15-4914-4c58-a721-abdc5b71b7fc>\",\"WARC-IP-Address\":\"104.21.92.86\",\"WARC-Target-URI\":\"https://favtutor.com/blogs/xor-python\",\"WARC-Payload-Digest\":\"sha1:NAQU7X6YYEC4F3GVQURGNT4U7MN5ZZYK\",\"WARC-Block-Digest\":\"sha1:TC4NXMQCVATCOJC62TXRIP7B4GSAHA7T\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510368.33_warc_CC-MAIN-20230928063033-20230928093033-00021.warc.gz\"}"}