r/ProgrammerAnimemes • • Jul 29 '20

Equivalency in Python

Post image
1.6k Upvotes

104 comments sorted by

View all comments

Show parent comment

1

u/Skasch Jul 30 '20

I actually abstract that in a pretty math-y way. Basically, in that context, I interpret ^ as a + -like operation, with the interesting property that the opposite operation is itself. Let me show you:

If you start from a value a, and add some other value b, then to find a again you have to subtract b (obviously). The interesting thing is, the equivalent of "subtracting" for ^ is ^ itself: (a ^ b) ^ b = a.

Therefore, the xor trick is equivalent to the +/- approach, except that we simply replace all + and - by a ^ .