🔌
nand2tetris
  • Introduction
  • Combinational Chips
    • Nand Gate
    • Not Gate
    • And Gate
    • Or Gate
    • Xor Gate
    • Multiplexor Chip
    • Demultiplexor Chip
    • Not16 Chip
    • And16 Chip
    • Or16 Chip
    • Mux16 Chip
    • Or8Way Chip
    • Mux4Way16 Chip
    • Mux8Way16 Chip
    • DMux4Way Chip
    • DMux8Way Chip
    • Half Adder Chip
    • Full Adder Chip
    • Add16 Chip
    • Inc16 Chip
    • Half Subtractor Chip
    • Full Subtractor Chip
    • Subtract16 Chip
    • Dec16 Chip
    • Arithmetic Chip
    • ALU
  • Misc
    • Int2Bool
    • Bool2Int
    • Arrayto16
Powered by GitBook
On this page
  • Xor Gate
  • Implementation of Xor Gate in HDL
  • Implementation of Xor Gate in Java™
  1. Combinational Chips

Xor Gate

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

PreviousOr GateNextMultiplexor Chip

Last updated 3 years ago

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).

Abstraction of Xor Gate - Representation and Truth Table

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);
    }
}
Implementation of Xor Gate using two Not gates, two And gates and an Or gate.
Implementation of Xor Gate using four Nand Ga
Implementation of Xor using Or, Nand and And gates.
Another Implementation of Xor Gate using two Not gates, two Or gates and an And gate.