XOR from only OR and AND

"The systems ({T, F}, and) and ({T, F}, or) are monoids."

"The system ({T, F}, xor) is an abelian group" which has the property of invertibility unlike monoids.

Therefore, 'and' and 'or' fail to construct 'xor' operation.

Source: https://en.wikipedia.org/wiki/Exclusive_or#Relation_to_modern_algebra


(a XOR b) = ((a OR b) - (a AND b)), or in other words, the union set minus the intersection set.

Code example (in javascript):

var a = 5;
var b = 12;
var xor = (a | b) - (a & b); // result: 9

Truth table for AND

  A  B  AND
  T  T  T
  T  F  F
  F  T  F
  F  F  F
  

Truth table for OR

  A  B  OR
  T  T  T
  T  F  T
  F  T  T
  F  F  F
  

Truth table for XOR

  A  B  XOR
  T  T  F
  T  F  T
  F  T  T
  F  F  F
  

So, XOR is just like OR, except it's false if A and B are true.

So, (A OR B) AND (NOT (A AND B)), which is (A OR B) AND (A NAND B)

  A  B  OR  AND NAND [(A OR B) AND (A NAND B)]
  T  T  T    T    F        F
  T  F  T    F    T        T
  F  T  T    F    T        T
  F  F  F    F    T        F
  

Not sure if it can be done without NOT or NAND


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?