The explanation of the workings of the XOR function at the logic gate level is very simple.
In principle, we have as a minimum an input of 2 different bits.
The XOR circuit will return an 1 as a result of operating two bits with different values.
At the input, we need an OR gate with two inputs.
We also need an AND gate with two inputs, and a negation gate in front of this AND to invert its result.
The first input pin of the OR gate is connected to the first input pin of the inverted AND gate, and the second pin of the OR gate is connected to the second input pin of the inverted AND gate.
In other words, we want to get the OR and the invertd AND results for the two input bits.
The two result bits we get from the OR and the negated AND gate are passed to a final AND gate with two inputs.
The result of this last AND gate will be the XOR value.
The intention of this circuit is to use the OR gate to detect the presence of a 1 and return a 1 as a result; and the intention of the negated AND gate is detect the presence of a 0 and return a 1 as a result.
The OR gates are oriented to return 1 when there is at least a 1 as input.
The AND gates are oriented to return 0 when there is at least a 0 as input. With the negation gate we get a 1 from the AND when tehre is a 0 present.
So with the OR we detect if there is a 1 present, and return 1.
With the AND we detect if there is a 0 present, and in that case we get a 0 which we negate to get a 1 in the presence of an input 0.
Now, if there is a 1 present, the OR will return 1.
And if there is a 0 present, the negated AND will return 1.
Let's take as example 11 binary.
When the OR gate processes it, we will get 1, which means that there is a 1 present at the input.
Now, when the AND gate processes the two original input bits set to 1, it will also return 1, but the negation gate in front of it will invert this result from 1 to 0 to indicate that there wasn't any 0 at the input.
When the 1 from the OR and the 0 from the negated AND get through the final AND gate, we will get a 0 in response of the original 11 binary input value. It will indicate that both input bits had the same value.
It's as if the XOR circuit was a question.: Are both input bits different? We will get a 1 indicating TRUE as a response if both input bits are different.
Now we can clearly see that if both input bits have the same value as in this case with 11 binary, the value returned by the XOR function will be 0.
Let's take another example, like 10 binary.
The OR gate takes both bits, 1 and 0, and returns 1 since there's at least a 1 present at the input.
The AND gate takes both bits 1 and 0, and returns 0 since there's at least a 0 present.
But with the negation gate in front of this AND we get a 1, which indicates that there was a 0 present at the input.
Now, the bit with value 1 from the OR result, and the bit with value 1 from the negated AND result enter the last AND, the output AND, the result AND, and since both bits are 1, we return 1.
What this indicates is that the OR took the input 1 and 0 and detected the presence of a 1, returning 1.
It also indicates that the negated AND took the input 1 and 0 and detected the presence of a 0, returning 1.
Since the OR and the negated AND gates indicate on their own both the presence of an input 1 and an input 0, respectively, we return a 1 for the XOR function of the whole circuit, to indicate that both bits were different.
As we can also see, at the most fundamental level we always operate the gates two by two, so solving will be easy. Even in the normal arithmetic we operate results two by two at the simplest logical level.
Step 1.: We need 2 input variables of at least 1 bit in size.
Step 2.: OR the values of both input variables.
Step 3.: AND the values of both variables and negate the bit values of this result.
Step 4.: Apply AND to the OR and the NAND results. This is the XOR value.
Most programming languages and assemblers have a XOR operator or instruction. It's very useful to generate shorter code which assigns 0 to a variable or register by applying XOR using its own value. As we have seen, since all bits will be the same by using the same processor register or memory variable as the first and second XOR operands, all bits will be cleared to 0 to indicate that they all had the same values.
function XOR(bits_A, bits_B)
/* Step 1: We need 2 input variables of at least 1 bit in size. */
var OR_0 = bits_A|bits_B; /* Step 2: OR the values of both input variables. */
var NAND_0 = ~(bits_A&bits_B); /* Step 3: AND the values of both variables and negate the bit values of this result. */
var XOR_AND_OUT = OR_0&NAND_0; /* Step 4: Apply AND to the OR and the NAND results. This is the XOR value. */