I am writing this post to answer a question from a student who studies IT at school.
They had this MCQ question where this Python code of XORing two integers several times and finding the output.
Question
a = 20
b = 30
a = a ^ b
b = a ^ b
a = a ^ b
XOR of two numbers - Hard way
First I'll explain how to do this properly for the sake of completion. If you know this already, you can just skip this and follow the easy answer.
First of all, it was not a computer-based exam, you have to think this through in your mind using pen and paper.
Second of all, how on earth do you apply XOR into integers? It's been a while I learned boolean algebra and I'm pretty sure boolean means true or false.
Then it kicked me, boolean also means 1s and 0s, where you can represent integers using 1s and 0s or binary and then apply the XOR. In fact, that's how actually the Python compiler does it. Let's try to XOR two integers, 20 and 30.
20 in base 10 can be converted to binary. You can convert a number into binary by keep dividing a number by 2 and deriving the remainders.
So, the binary of 30 is 11110.
If we apply this to the above two numbers;
XOR of two numbers - Easy way
While converting back and forth binary to decimal above, you may have noticed, binary is always about representing a number with powers of 2.
Let's try to do that without converting it into 1s and 0s.
Let us consider 20
First, find out the largest power of 2 closer to 20. It is 16. Let's write it down.
16
Then, in order to make 20, how much we need? It's 4. And what's the largest power of 2 closer to 4. 4 itself is a power of 2. Let's write it down also.
16, 4
If we add them together, we get 20. So, 20 can be represented by 16 + 4.
Similarly, Let's consider 30.
What's the largest power of 2 closer to 30? It is again 16. Let's write it down.
16.
16 + 4 XOR 16 + 8 + 4 + 2
Let's add what is left over.
8 + 2 = 10
It may be confusing at first, but practice it a couple of times, you'll get the hang of it.
Now, let's try to solve the above problem with this technique.
Solving the question
first, a = 20 and b = 30
a = a^b -> 16 + 4 XOR 16 + 8 + 4 + 2 -> 10
now, a = 10 and b = 30
b = a^b -> 8 + 2 XOR 16 + 8 + 4 + 2 -> 20
now, a = 10 and b = 20
a = a^b -> 8 + 2 XOR 16 + 4 -> 30
You may notice that there is nothing common to cut off. So we add them all together.
If you are working on an MCQ question, this would help you to save a couple of minutes!
The trick of the question
If you check the question, what it gives two values and apply XOR over and over three times. Surprisingly the outcome is the values have been swapped.
The trick of this question is it is true for any couple of values, if you XOR it 3 times, the original values are being swapped.
Just to be sure, let's get some random two numbers and see if it is true. I'm using the Python interpreter because I'm too lazy to write it down and do it manually.
Just to be sure let's run this in Python
That's all folks! Try out if the above little trick can be applied to other boolean operations also!
Comments
Post a Comment