bitwise-operatorsxorbitwise-and

XOR from only OR and AND


How do you do the XOR bitwise operation if you only have available the AND and the OR operations?


Solution

  • Creating my own scripting language - ChrisScript - you just need something like:

    #!/bin/chrish
    
    bit XOR (bit A, bit B)
    {
       bit notA;
       bit notB;
    
       IF (A == 0) notA = 1 ELSE notA = 0;
       IF (B == 0) notB = 1 ELSE notB = 0;
    
       F = ((A && notB) || (notA && B));
    
       RETURN F;
    }
    

    Even without NOT, it can be emulated like this. But this is the best solution you're going to get without having some form of inverter. I find it hard to believe you don't have some form of inverter availble -- what scripting environment are you using?