Skip to main content

Posts

Showing posts with the label Boolean algebra

Apply XOR operator for integers

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 The question is to find the output results of a and b. If I type this code in the Python interpreter, I get the below outputs. 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 actua...