XOR using NAND gates

Create an XOR gate using only NAND gates.  (BTW, mostly all circuit problems assume you have two inputs plus 0 and 1 also as inputs).

Solution

You may be able to find the solution on the discussion forum.

2026 Update: Logic Gate Design — NAND is Functionally Complete

NAND is a functionally complete gate: you can build any Boolean function using only NAND gates. This is why modern CPUs are built from NAND gates at the transistor level.

Building XOR from NAND: XOR(A,B) = (A NAND (A NAND B)) NAND (B NAND (A NAND B)). This takes 4 NAND gates.

def NAND(a, b):
    return not (a and b)

# NOT using NAND: NOT(A) = NAND(A, A)
def NOT(a):
    return NAND(a, a)

# AND using NAND: AND(A,B) = NOT(NAND(A,B))
def AND(a, b):
    return NOT(NAND(a, b))

# OR using NAND: OR(A,B) = NAND(NOT(A), NOT(B)) [De Morgan's law]
def OR(a, b):
    return NAND(NOT(a), NOT(b))

# XOR using 4 NAND gates
def XOR_via_NAND(a, b):
    nand_ab = NAND(a, b)           # Gate 1
    nand_a_nab = NAND(a, nand_ab)  # Gate 2
    nand_b_nab = NAND(b, nand_ab)  # Gate 3
    return NAND(nand_a_nab, nand_b_nab)  # Gate 4

# Verify truth table
print("A  B  XOR  NAND-based")
for a in [False, True]:
    for b in [False, True]:
        expected = a ^ b
        result = XOR_via_NAND(a, b)
        print(f"{int(a)}  {int(b)}  {int(expected)}    {int(result)}  {'✓' if expected == result else '✗'}")

# Gate count optimization:
# XOR from 4 NANDs  (minimum proven optimal)
# XOR from 5 NORs
# XOR from 7 AND/OR/NOT gates without restriction

# Real-world: NAND flash memory uses the same principle —
# NAND gates are cheaper to manufacture than AND gates
print("nNAND flash: the same NAND that's functionally complete")
print("is also the dominant storage technology in SSDs and phones")

Interview angle: This question appears in hardware design, FPGA programming, and systems interviews. The deeper question is: “Why is NAND universal?” Answer: because any truth table can be expressed as AND, OR, NOT — and all three can be built from NAND. Connects to computational universality and why a single primitive operation (like NAND or NOR) can compute anything.

💡Strategies for Solving This Problem

Logic Gates and Boolean Algebra

Got this at a systems programming interview. Tests understanding of digital logic and boolean algebra. Important for low-level programming and computer architecture.

The Problem

Implement XOR using only NAND gates. NAND is "universal" - you can build any logic gate from it.

Background

NAND truth table:

A  B  | NAND
0  0  |  1
0  1  |  1
1  0  |  1
1  1  |  0

XOR truth table:

A  B  | XOR
0  0  |  0
0  1  |  1
1  0  |  1
1  1  |  0

Why NAND is Universal

You can build NOT, AND, OR from NAND:

  • NOT A: NAND(A, A)
  • AND: NOT(NAND(A, B))
  • OR: NAND(NOT A, NOT B)

Building XOR

XOR = (A AND NOT B) OR (NOT A AND B)

So we need to express this using only NAND.

The Solution Pattern

XOR can be built with 4 NAND gates:

  1. nand1 = NAND(A, B)
  2. nand2 = NAND(A, nand1)
  3. nand3 = NAND(B, nand1)
  4. result = NAND(nand2, nand3)

Why It Works

Let's trace through:

  • nand1 = NAND(A, B) = NOT(A AND B)
  • nand2 = NAND(A, nand1) = NOT(A AND NOT(A AND B)) = NOT A OR B
  • nand3 = NAND(B, nand1) = NOT(B AND NOT(A AND B)) = NOT B OR A
  • result = NAND(nand2, nand3) = NOT((NOT A OR B) AND (NOT B OR A))

Through boolean algebra, this reduces to A XOR B.

Alternative: Using 5 NAND Gates

First build NOT, AND, OR, then combine:

  1. NOT A = NAND(A, A)
  2. NOT B = NAND(B, B)
  3. Then use formula: XOR = (A AND NOT B) OR (NOT A AND B)

This uses 5 gates but is easier to understand.

Scroll to Top