Search code examples
mathbit-manipulationbit

Why does this 8 bit binary (100000010) stayed the same using XOR operation when masking?


I apologize if this is in the wrong section. As for my question, I do understand the AND and OR masking operations. I am quite confused why does 10000010 have the same mask's value as the original value? We were asked to toggle the bits 1 and 7. If I do understand XOR right, I'd toggle 1 to 0 and 0 to 1. Why the mask of 10000010 isn't 00000000? I appreciate any helpful explanation regarding this question.

I have this one class asynchronous, so asking my prof during class time is not possible. I have emailed my professor already, but he hasn't replied for the past few hours.


Solution

  • XOR is a function that takes 2 inputs and apply an "EXCLUSIVE OR" (XOR) meaning it equals 1 only if the inputs are different:

    0 XOR 0 = 0
    0 XOR 1 = 1
    1 XOR 0 = 1
    1 XOR 1 = 0
    

    That means that bit to bit you have:

    10000010 XOR 00000000 = 10000010
    

    If you want to get 00000000, you should go:

    10000010 XOR 10000010 = 00000000