Earlier this month, I was presented with an opportunity to interview interns that want to join my company. One of the interviewee really stood out. When my interviewer colleague asked him how to switch the value of variable, we expected him to just use the normal math operand like +
and -
:
variable_1 = 10
variable_2 = 50
print("=========BEFORE SWITCH=========")
print("variable_1: ", variable_1)
print("variable_2: ", variable_2)
variable_1 = variable_1 + variable_2
variable_2 = variable_1 - variable_2
variable_1 = variable_1 - variable_2
print("=========AFTER SWITCH=========")
print("variable_1: ", variable_1)
print("variable_2: ", variable_2)
But instead, he used an XOR function. Here's the code that he basically wrote:
variable_1 = 10
variable_2 = 50
print("=========BEFORE SWITCH=========")
print("variable_1: ", variable_1)
print("variable_2: ", variable_2)
variable_1 = variable_1 ^ variable_2
variable_2 = variable_1 ^ variable_2
variable_1 = variable_1 ^ variable_2
print("=========AFTER SWITCH=========")
print("variable_1: ", variable_1)
print("variable_2: ", variable_2)
Result:
=========BEFORE SWITCH=========
variable_1: 10
variable_2: 50
=========AFTER SWITCH=========
variable_1: 50
variable_2: 10
That made me think, what is XOR and how does it work?
I'm sure that you've already heard or learn about XOR. But, I'm sure that you've forgotten about it already. Let's get to the basic and learn it once again!
What is Exclusive Or
XOR, or Exclusive Or, is a logical gate that return the value 1 if the parameters is different, and return 0 when the parameters is the same.
We can visualize the XOR to be like:
We can also represent exclusive or in the table:
| A | B | Result |
|---|---|--------|
| 0 | 0 | 1 |
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 1 | 1 |
Swapping variable with Exclusive Or
Now let's try doing an exclusive or to a String "ab" and "ba".
We can represent "ab" in binary as: 01100001 01100010
For "ba" we can also represent it in binary as: 01100010 01100001
so if we do Exclusive or 01100001 01100010 ^ 01100010 01100001
the result will be 11111100 1111100
, both exclusive or produce the same value because we basically did an exclusive or two times between "a" and "b".
Now let's try doing an exclusive or between "ab" and our new binary and see what's the result.
01100001 01100010 ^ 11111100 1111100
= 01100010 01100001
You probably already guessed this. Yes, the result is "ba" which is the String that we used to make our new binary.
In the previous example we only used 2 length string with 2 char. Does this method can be used for a string with longer length and more varied characters? Let's try it.
variable_1 = "how are you" = 01101000 01101111 01110111 00100000 01100001 01110010 01100101 00100000 01111001 01101111 01110101
variable_2 = "hello, world" = 01101000 01100101 01101100 01101100 01101111 00100000 01110111 01101111 01110010 01101100 01100100
If we do an exclusive or with them, we'll get: 00000000 00001010 00011011 01001100 00001110 01010010 00010010 01001111 00001011 00000011 00010001
Yes, it still works. If you try to do an exclusive or between variable_1 and Exclusive Or result, you'll get variable_2. Also the same, if you try to do an exclusive or between variable_2 and the result, you'll get variable_3.
References
https://en.wikipedia.org/wiki/Exclusive_or#:~:text=Exclusive%20or%20or%20exclusive%20disjunction,%E2%8A%95%2C%20%E2%86%AE%2C%20and%20%E2%89%A2.
https://en.wikipedia.org/wiki/XOR_gate
Top comments (0)