XOR gate


The XOR gate is a digital logic gate that gives a true output when the number of true inputs is odd. An XOR gate implements an exclusive or from mathematical logic; that is, a true output results if one, and only one, of the inputs to the gate is true. If both inputs are false or both are true, a false output results. XOR represents the inequality function, i.e., the output is true if the inputs are not alike otherwise the output is false. A way to remember XOR is "must have one or the other but not both".
An XOR gate may serve as a "programmable inverter" in which one input determines whether to invert the other input, or to simply pass it along with no change. Hence it functions as a inverter which may be activated or deactivated by a switch.
XOR can also be viewed as addition modulo 2. As a result, XOR gates are used to implement binary addition in computers. A half adder consists of an XOR gate and an AND gate. The gate is also used in subtractors and comparators.
The algebraic expressions or or or all represent the XOR gate with inputs A and B. The behavior of XOR is summarized in the truth table shown on the right.

Symbols

There are three schematic symbols for XOR gates: the traditional ANSI and DIN symbols and the IEC symbol. In some cases, the DIN symbol is used with ⊕ instead of ≢. For more information see Logic gate symbols.
The "=1" on the IEC symbol indicates that the output is activated by only one active input.
class=skin-invertclass=skin-invertclass=skin-invert
ANSI XOR schematic symbol IEC XOR schematic symbol DIN XOR schematic symbol

The logic symbols ⊕, Jpq, and ⊻ can be used to denote an XOR operation in algebraic expressions.
C-like languages use the caret symbol ^ to denote bitwise XOR.

Implementation

The XOR gate is most commonly implemented using MOSFETs circuits. Some of those implementations include:

AND-OR-invert

XOR gates can be implemented using AND-OR-invert or OR-AND-invert logic.

CMOS

The metal–oxide–semiconductor implementations of the XOR gate corresponding to the AOI logic above are shown below.
On the left, the nMOS and pMOS transistors are arranged so that the input pairs and activate the 2 pMOS transistors of the top left or the 2 pMOS transistors of the top right respectively, connecting Vdd to the output for a logic high. The remaining input pairs and activate each one of the two nMOS paths in the bottom to Vss for a logic low.
If inverted inputs are available, this gate can be used directly. Otherwise, two additional inverters with two transistors each are needed to generate and, bringing the total number of transistors to twelve.
The AOI implementation without inverted input has been used, for example, in the Intel 386 CPU.

Transmission gates

The XOR gate can also be implemented through the use of transmission gates with pass transistor logic.
This implementation uses two transmission gates and two inverters not shown in the diagram to generate and for a total of eight transistors, four less than in the previous design. The XOR function is implemented by passing through to the output the inverted value of A when B is high and passing the value of A when B is at a logic low. so when both inputs are low the transmission gate at the bottom is off and the one at the top is on and lets A through which is low so the output is low. When both are high only the one at the bottom is active and lets the inverted value of A through and since A is high the output will again be low. Similarly if B stays high but A is low the output would be which is high as expected and if B is low but A is high the value of A passes through and the output is high completing the truth table for the XOR gate.
The trade-off with the previous implementation is that since transmission gates are not ideal switches, there is resistance associated with them, so depending on the signal strength of the input, cascading them may degrade the output levels.

Optimized pass-gate-logic wiring

The previous transmission gate implementation can be further optimized from eight to six transistors by implementing the functionality of the inverter that generates and the bottom pass-gate with just two transistors arranged like an inverter but with the source of the pMOS connected to instead of Vdd and the source of the nMOS connected to instead of GND.
The two leftmost transistors mentioned above, perform an optimized conditional inversion of A when B is at a logic high using pass transistor logic to reduce the transistor count and when B is at a logic low, their output is at a high impedance state. The two in the middle are a transmission gate that drives the output to the value of A when B is at a logic low and the two rightmost transistors form an inverter needed to generate used by the transmission gate and the pass transistor logic circuit.
As with the previous implementation, the direct connection of the inputs to the outputs through the pass gate transistors or through the two leftmost transistors, should be taken into account, especially when cascading them.

XOR with AND and NOR

Replacing the second NOR with a normal OR gate will create an XNOR gate.

Alternatives

If a specific type of gate is not available, a circuit that implements the same function can be constructed from other available gates. A circuit implementing an XOR function can be trivially constructed from an XNOR gate followed by a NOT gate. If we consider the expression, we can construct an XOR gate circuit directly using AND, OR and NOT gates. However, this approach requires five gates of three different kinds.
As alternative, if different gates are available we can apply Boolean algebra to transform as stated above, and apply de Morgan's law to the last term to get which can be implemented using only four gates as shown on the right. intuitively, XOR is equivalent to OR except for when both A and B are high. So the AND of the OR with then NAND that gives a low only when both A and B are high is equivalent to the XOR.
An XOR gate circuit can be made from four NAND gates. In fact, both NAND and NOR gates are so-called "universal gates" and any logical function can be constructed from either NAND logic or NOR logic alone. If the four NAND gates are replaced by NOR gates, this results in an XNOR gate, which can be converted to an XOR gate by inverting the output or one of the inputs.
Desired gateNAND constructionNOR construction
Image:XOR ANSI Labelled.svg

An alternative arrangement is of five NOR gates in a topology that emphasizes the construction of the function from, noting from de Morgan's Law that a NOR gate is an inverted-input AND gate. Another alternative arrangement is of five NAND gates in a topology that emphasizes the construction of the function from, noting from de Morgan's Law that a NAND gate is an inverted-input OR gate.
Desired gateNAND constructionNOR construction
Image:XOR ANSI Labelled.svg
Image:XOR ANSI Labelled.svg--

For the NAND constructions, the upper arrangement requires fewer gates. For the NOR constructions, the lower arrangement offers the advantage of a shorter propagation delay.

Standard chip packages

XOR chips are readily available. The most common standard chip codes are:
  • 4070: CMOS quad dual input XOR gates.
  • 4030: CMOS quad dual input XOR gates.
  • 7486: TTL quad dual input XOR gates.

    More than two inputs

Literal interpretation of the name "exclusive or", or observation of the IEC rectangular symbol, raises the question of correct behaviour with additional inputs. If a logic gate were to accept three or more inputs and produce a true output if exactly one of those inputs were true, then it would in effect be a one-hot detector. However, it is rarely implemented this way in practice.
It is most common to regard subsequent inputs as being applied through a cascade of binary exclusive-or operations: the first two signals are fed into an XOR gate, then the output of that gate is fed into a second XOR gate together with the third signal, and so on for any remaining signals. The result is a circuit that outputs a 1 when the number of 1s at its inputs is odd, and a 0 when the number of incoming 1s is even. This makes it practically useful as a parity generator or a modulo-2 adder.
For example, the 74LVC1G386 microchip is advertised as a three-input logic gate, and implements a parity generator.

Applications

XOR gates and AND gates are the two most-used structures in VLSI applications.

Addition

The XOR logic gate can be used as a one-bit adder that adds any two bits together to output one bit. For example, if we add 1 plus 1 in binary, we expect a two-bit answer, 10. Since the trailing sum bit in this output is achieved with XOR, the preceding carry bit is calculated with an AND gate. This is the main principle in half adders. A slightly larger full adder circuit may be chained together in order to add longer binary numbers.
In certain situations, the inputs to an OR gate or to an XOR gate can never be both 1's. As this is the only combination for which the OR and XOR gate outputs differ, an OR gate may be replaced by an XOR gate without altering the resulting logic. This is convenient if the circuit is being implemented using simple integrated circuit chips which contain only one gate type per chip.

Pseudo-random number generator

, specifically linear-feedback shift registers, are defined in terms of the exclusive-or operation. Hence, a suitable setup of XOR gates can model a linear-feedback shift register, in order to generate random numbers.