Xor Gate

Abstraction and Implementation of Xor Gate in Hardware Design Language and Java™.

Xor Gate

The Xor function, also known as Exclusive-OR, returns 1 (true) when either of the inputs are same (both are 0 or both are 1 simultaneously).

Implementation of Xor Gate in HDL

Xor Gate can be implemented in four ways: (in fact, they are simplifications by DeMorgan's laws.)

  • Xor Gate can be implemented using Not, And and Or gates implemented previously as follows:

  • Another way to implement is to substitute all gates with basic Nand gates, eventually ending up with four Nand gates.

  • We can simply the expression of first implementation of Xor gate as follows:

Output = A'B + B'A

= A'A + A'B + B'A + B'B (since A'A = B'B = 1)

= (A + B)(A' + B')

  • In the expression of third implementation of Xor gate, A' + B' can be rewritten as (AB)' [ Nand gate ] using DeMorgan's laws.

(A + B)(A' + B') = (A + B)(AB)'

CHIP Xor {
    IN a, b;
    OUT out;

    PARTS:
    Not(in=a, out=nota);
    Not(in=b, out=notb);
    Or(a=a, b=notb, out=aOrnotb);
    Or(a=nota, b=b, out=notaOrb);
    And(a=aOrnotb, b=notaOrb, out=out);
}

Implementation of Xor Gate in Java™

Similar to the implementations in HDL. While we're not going to implement every implementation, you can try other implementations to see if it works.

package CombChips;

class Xor_Gate extends Or_Gate {

    protected static int Xor(int a, int b) {
        int OrofNots = Or(Not(a), Not(b));
        int Or = Or(a, b);
        return And_Gate.And(Or, OrofNots);
    }
}

Last updated