0

how xor(^) operator works here? pls explain.....

c=[1,4,1] d=0 for j in c: d=d^j print(d) #how come the result: 4 ??

10th May 2018, 6:57 AM
Tincture
1 Odpowiedź
+ 4
Xor (esclusive or) is a bitwise operator that, to bit level, return true/1 if operands are different else false/0... In numeric context, it apply this at all bits in operands following order (apply to either first bits, to either second bits and so on)... At example: 4^2= 4= 100 2= 010 ________ 110 = 6 On your problem, you are looping on array and for any item (number) , you do xor with last result (at begin is 0) then: 1st cycle: 1^0= 1 ==> save 1 2nd cycle: 1^4= 5 ==> save 5 3rd cycle= 5^1= 4 ==> save 4 print saved ==> 4
10th May 2018, 9:02 AM
KrOW
KrOW - avatar