What is the xor of 0b011 and 0b101 in python?

While reviewing the solution of a problem from the Python Data Structure and Algorithm course I am taking on Udemy, I got confused at the concept of "XOR".

As you can see below, we started off with 2 lists- arr1 and arr2, and then the solution basically tries to find the number in arr1 that is not in arr2 (which is 5 in this example). The "XOR" appears in the line "result^=num", and I have no idea how the "result" variable changes based on value of "num".

arr1 = [1,2,3,4,5,6,7]
arr2 = [3,7,2,1,4,6]

result = 0

for num in arr1+arr2:
    print('result is ' + str(result))
    print('num is ' + str(num))
    result^=num
    print('result is ' + str(result))
    print()

print()    
print('final result is ' + str(result))  

Output:

result is 0
num is 1
result is 1

result is 1
num is 2
result is 3

result is 3
num is 3
result is 0

result is 0
num is 4
result is 4

result is 4
num is 5
result is 1

result is 1
num is 6
result is 7

result is 7
num is 7
result is 0

result is 0
num is 3
result is 3

result is 3
num is 7
result is 4

result is 4
num is 2
result is 6

result is 6
num is 1
result is 7

result is 7
num is 4
result is 3

result is 3
num is 6
result is 5


final result is 5

I know for example when input 1 equals input 2 then XOR will become "0". If input 1 is 0 and input 2 is 1 OR if input 1 is 1 and input 2 is 0 then XOR will return "1". But I don't understand is what if input 1 and input 2 are not the same and they are not 1 or 0. Let's say when input 1 is 10 and input 2 is 15 OR if input 1 is 15 and input 2 is 10 then XOR will return "5". But why is "5" the XOR output in this case?

Any idea to help me better understand the relationship between inputs and their XOR outputs will be greatly appreciated, hopefully in a not-so-technical manner!

What is the xor of 0b011 and 0b101 in python?

Bitwise operators are used for performing operations on operations on Binary pattern or Bit sequences. Python has 6 bitwise operators: AND, OR, XOR, Complement and Shift Operators. They normally operate on numbers but instead of treating them as numbers they are treated as string of bits, written in twos complement binary by the operators.

Let’s go through go through a brief summary of twos compliment to make you understand better:

  • Binary Representation
    • Two’s Representation of Positive numbers
    • Two’s Representation of Negative numbers
  • BitWise Operators Table
  • Explanation Time
    • Bitwise NOT/Complement Operator
    • Bitwise AND Operator
    • Bitwise OR Operator
    • Bitwise XOR Operator
    • Leftward Shift Operator
    • Rightward Shift Operator
  • Practice Makes you perfect!!

Binary Representation

Two’s Representation of Positive numbers

Positive numbers have a pretty straight forward representation:

  • 0 can be represented as ‘0’
  • 1 can be represented as ‘1’
  • 2 can be represented as ’10’
  • 3 can be represented as ’11’
  • 1025 is ‘10000000001’ is == 2**10 + 2**0 == 1024 + 1

Two’s Representation of Negative numbers

Negative numbers are written with a leading one instead of a leading zero. So if you are using only 8 bits for your twos-complement numbers, then you treat patterns from “00000000” to “01111111” as the whole numbers from 0 to 127, and reserve “1xxxxxxx” for writing negative numbers.

Using only 8 bits for twos-compliment negative numbers can be represented as:

  • -x can be represented as the complement of the bit pattern for (x-1)
  • So -2 is the ~(2-1) == ~(1) == “11111101”

Python doesn’t use 8-bit numbers. It USED to use however many bits were native to your machine, but since that was non-portable, it has recently switched to using an INFINITE number of bits. Thus the number -5 is treated by bitwise operators as if it were written: “…1111111111111111111011”.

Phew!! Now that the hard part is over lets jump to Bitwise Operators


BitWise Operators Table

Operator Representation Example
AND a & b Does a “bitwise and“. Each bit of the output is 1 if the corresponding bit of a AND of b is 1, otherwise it’s 0.
OR a | b Does a “bitwise or”. Each bit of the output is 0 if the corresponding bit of a AND of b is 0, otherwise it’s 1.
XOR a ^ b Does a “bitwise exclusive or“. Each bit of the output is the same as the corresponding bit in a if that bit in b is 0, and it’s the complement of the bit in a if that bit in b is 1.
Compliment ~ a Returns the complement of a – the number you get by switching each 1 for a 0 and each 0 for a 1. This is the same as -a – 1.
Left Shift a << b Returns a with the bits shifted to the left by b places (and new bits on the right-hand-side are zeros). This is the same as multiplying a by 2**b.
Right Shift a >> b Returns a with the bits shifted to the right by y places. This is the same as //’ing a by 2**b.

Explanation Time

Bitwise NOT/Complement Operator

The bitwise NOT, or complement, is a unary operation that performs logical negation on each bit, forming the ones’ complement of the given binary value. Bits that are 0 become 1, and those that are 1 become 0. Let’s see a few Examples:

NOT x = -x − 1

>>> bin(3)
'0b11'
>>> bin(~3)
'-0b100'
>>> ~3
-4

Bitwise not operator can also be used to invert a gray scale image which is stored as an unsigned integer.


Bitwise AND Operator

A bitwise AND takes two equal-length binary representations and performs the logical AND operation on each pair of the corresponding bits, which is equivalent to multiplying them. Thus, if both bits in the compared position are 1, the bit in the resulting binary representation is 1 (1 × 1 = 1); otherwise, the result is 0 (1 × 0 = 0 and 0 × 0 = 0). For example:

>>> bin(0b110 & 0b011) # 6 & 3
'0b10'
>>> 0b110 & 0b011	
2
>>> 6 & 3	
2

AND Operator can be used to quickly test divisibility by 2

>>> 6 & 1 # Even
0
>>> 8 & 1 # Even
0
>>> 11 & 1 # Odd
1

Bitwise OR Operator

A bitwise OR takes two bit patterns of equal length and performs the logical inclusive OR operation on each pair of corresponding bits. The result in each position is 0 if both bits are 0, while otherwise the result is 1. For example:

>>> bin(0b110 | 0b011) # 6 | 3
'0b111'
>>> 0b110 | 0b011
7
>>> 6 | 3
7

Bitwise XOR Operator

A bitwise XOR takes two-bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. In this we perform the comparison of two bits, being 1 if the two bits are different, and 0 if they are the same. For example:

>>> bin(0b110 ^ 0b011) # 6 ^ 3
'0b101'
>>> 0b110 ^ 0b011
5
>>> 6 ^ 3
5

Assembly language programmers and optimizing compilers sometimes use XOR as a short-cut to setting the value of a register to zero. Performing XOR on a value against itself always yields zero, and on many architectures this operation requires fewer clock cycles and memory than loading a zero value and saving it to the register.


Leftward Shift Operator

A leftward Shift Operator shifts the bits left y places and zeroes are shifted in to fill the discarded bits. Let’s see an image that will make you understand it better:

What is the xor of 0b011 and 0b101 in python?

Here the bits are shifted 1 place and a zero is filled at the end. This is same as multiplying a number by 2. See for yourself in this example:

>>> 3<<1  # x< 3*2
6
>>> 3<<2  # 3*2**2
12
>>> 3<<2  # 3*2**3
24
>>> bin(3)
'0b11'
>>> bin(6)
'0b110'

Rightward Shift Operator

A Rightward Shift Operator shifts the bits right y places and zeroes are shifted in to fill the discarded bits. Let’s see an image that will make you understand it better:

What is the xor of 0b011 and 0b101 in python?

Here the bits are shifted 1 place and a zero is filled at the end. This is same as //ing a number by 2. Let’s see few examples to make you understand better:

>>> bin(3)
'0b11'
>>> bin(6)
'0b110'
>>> 6>>1  # '110' -> '011' 1 place shift
3
>>> 6>>2  # '110' -> '001' 2 place shift
1
>>> 6//2
3
>>> 6//4
1

Voila!! We are done. In case if you face any problem please comment below. I will be happy to help.

Practice Makes you perfect!!

Here are few questions which you can use to practice bitwise operators problems:

  • The Flipping Coin Problem
  • Number of Factors

More questions are added daily with its approach and best solution. Subscribe to our site to be updated for new posts and keep learning.

How is XOR calculated 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.

How do you do XOR of two strings in Python?

How to take the bitwise XOR of two strings in Python.
s1 = "abc".
s2 = "a23".
a_list = [chr(ord(a) ^ ord(b)) for a,b in zip(s1, s2)].
print(a_list).
s3 = "". join(a_list).
print(s3).

What is the difference between 5 and 7 and 5 & 7 Python?

So, here, 5 and 7 is the same as True and 7.

What is bitwise eXclusive or for in Python?

Bitwise XOR (^) operator will take two equal length binary sequence and perform bitwise XOR operation on each pair of bit sequence. XOR operator will return 1, if both bits are different. If bits are same, it will return 0.