-1

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!

Stanleyrr
  • 858
  • 3
  • 12
  • 31

2 Answers2

2

XOR works on binary. It only returns 1, if one number has a 1 in the same bit place as anothers zero. So in this exampe:

In [8]: bin(5)                                                                                                                                                                                             
Out[8]: '0b101'

In [9]: bin(6)                                                                                                                                                                                             
Out[9]: '0b110'

In [13]: 5^6                                                                                                                                                                                               
Out[13]: 3

In [10]: bin(3)                                                                                                                                                                                            
Out[10]: '0b11'

the leftmost bit's are 1, so they zero out. The next values are 1 and 0, so you receive 1 for the next two bits, leading to the answer 3 in the case of 5^6.

When XORing for each 1 and zero that lines up, you can solve the answer by doing this operation at each bit:

0^0 is 0
1^0 is 1
1^1 is 0


5 is 0b101
6 is 0b110

so for 5^6 = 3 

the answer is
3 is 0b011
oppressionslayer
  • 6,942
  • 2
  • 7
  • 24
1

XOR is a bit operation. It's will process on each bit.

0 xor 0 = 0
0 xor 1 = 1
1 xor 0 = 1
1 xor 1 = 0

For example 6 is 0b110 3 is 0b11, so 6 xor 3 is 0b101 which is 5

LF00
  • 27,015
  • 29
  • 156
  • 295