cbitwise-operatorsboolean-algebra

Bitwise Operators: Using only & and ~ to get ^


I've been stuck on a bonus my professor gave for a couple of days now:

I've tried many different combinations, and also tried to write out the logic of the operator ^, but it hasn't been working out. Any hints or help would be much appreciated!


Solution

  • The XOR operator can in fact be written as a combination of those two. I'll put this in two steps:

    A NAND B = NOT(A AND B)

    A XOR B = (A NAND (A NAND B)) NAND (B NAND (A NAND B))

    As described before on math:

    https://math.stackexchange.com/questions/38473/is-xor-a-combination-of-and-and-not-operators