Chapter 1

What Can a Single Switch Do

What Can a Single Switch Do

Think about the light switch on your wall. Flip it up, the light comes on. Flip it down, darkness. It's so ordinary that you've probably never thought twice about it. But here's something you might not have considered: that simple on/off action is the fundamental building block of every computer ever made. The billions of transistors inside your phone and laptop are doing exactly the same thing as your light switch โ€” just a few billion times per second.

The only real difference is that your light switch needs a finger. A transistor responds to an electrical signal and can switch on and off in a fraction of a nanosecond.

Core Principles

From Switches to Logic Gates

A single switch can only represent two states: on (1) and off (0). That sounds pretty limited. But when you start combining switches together, something remarkable happens.

AND Gate: Two switches in series

Wire two switches A and B in series โ€” one after the other in a single path:

Power --[Switch A]--[Switch B]-- Light -- Ground

Current can only flow if both A and B are closed. The light only turns on when A and B are on. That's AND logic.

A B Light (A AND B)
0 0 0
0 1 0
1 0 0
1 1 1

OR Gate: Two switches in parallel

Now wire A and B in parallel โ€” two separate paths for current to travel:

        โ”Œโ”€โ”€[Switch A]โ”€โ”€โ”
Power โ”€โ”€โ”ค               โ”œโ”€โ”€ Light โ”€โ”€ Ground
        โ””โ”€โ”€[Switch B]โ”€โ”€โ”˜

If either A or B is closed, current finds a path and the light turns on. This is OR logic โ€” either one is enough.

NOT Gate: Flipping the signal

The NOT gate is a stubborn little circuit. Feed it a 1, it outputs a 0. Feed it a 0, it outputs a 1. It simply inverts whatever it receives.

Input 1 โ†’ NOT โ†’ Output 0
Input 0 โ†’ NOT โ†’ Output 1

From Vacuum Tubes to Transistors

Early computers used vacuum tubes as their switches. Vacuum tubes looked like light bulbs and controlled current by heating a filament to emit electrons. They were huge, power-hungry, and burned out constantly. ENIAC, built in 1946, contained 17,468 vacuum tubes and filled an entire building.

In 1947, three scientists at Bell Labs invented the transistor. Made from semiconductor materials (typically silicon), a transistor uses a tiny control voltage to open or close the main current path โ€” in other words, it's an electrically controlled switch.

        Collector (C)
           |
  Base(B)โ”€โ”€โ”ค  โ† Control signal (tiny current)
           |
        Emitter (E)
           |
          Ground

Signal at B โ†’ C-to-E conducts (switch closed)
No signal at B โ†’ C-to-E blocks (switch open)

The revolutionary thing about transistors is that they can be made incredibly small, switch incredibly fast, and last essentially forever. A transistor in a modern CPU is just a few nanometers across โ€” far smaller than a virus.

Three Gates Are Enough to Build Everything

AND, OR, and NOT are all you need. Mathematically, it's been proven that any logical function โ€” no matter how complex โ€” can be built from combinations of these three gates.

There's also the NAND gate (Not-AND), which has a special property: NAND alone is functionally complete, meaning you can build any other gate using only NANDs. That's why chip designers love it โ€” a single primitive cell type is enough to construct an entire CPU.

NAND Truth Table:
  0 NAND 0 = 1
  0 NAND 1 = 1
  1 NAND 0 = 1
  1 NAND 1 = 0   โ† only false when both inputs are true

Build NOT from NAND:  NOT(A) = NAND(A, A)
Build AND from NAND:  AND(A,B) = NAND(NAND(A,B), NAND(A,B))

Hands-On Verification

Let's simulate all three basic gates in Python and verify the NAND-completeness claim:

# Three basic logic gates
def AND(a, b):
    return a & b

def OR(a, b):
    return a | b

def NOT(a):
    return 1 - a

# Build everything from NAND alone
def NAND(a, b):
    return NOT(AND(a, b))

def NOT_from_NAND(a):
    return NAND(a, a)

def AND_from_NAND(a, b):
    return NAND(NAND(a, b), NAND(a, b))

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

# Verify all combinations
print("Verifying AND vs AND_from_NAND:")
for a in [0, 1]:
    for b in [0, 1]:
        native = AND(a, b)
        rebuilt = AND_from_NAND(a, b)
        match = "OK" if native == rebuilt else "MISMATCH"
        print(f"  {a} AND {b}: native={native}, from_NAND={rebuilt} [{match}]")

Output:

Verifying AND vs AND_from_NAND:
  0 AND 0: native=0, from_NAND=0 [OK]
  0 AND 1: native=0, from_NAND=0 [OK]
  1 AND 0: native=0, from_NAND=0 [OK]
  1 AND 1: native=1, from_NAND=1 [OK]

This is the deep insight: software logic and hardware logic are the same thing expressed differently. A Python and statement and a physical NAND gate are two faces of the same mathematical coin.

๐Ÿ”ฌ Going Deeper

The Mathematical Foundation: Boolean Algebra

Logic gates rest on solid mathematics called Boolean algebra, developed by George Boole in 1854. For nearly a century it was considered pure mathematics with no practical application. Then in 1937, Claude Shannon โ€” then a 21-year-old MIT graduate student โ€” wrote his master's thesis showing that Boolean algebra could be directly implemented with electrical circuits. Many engineers consider it the most important master's thesis of the 20th century. Shannon later went on to found information theory, but this early work is what made digital computers possible.

From MOSFET to FinFET

The transistors in modern chips aren't the bipolar junction transistors (BJTs) from the 1940s โ€” they're MOSFETs (Metal-Oxide-Semiconductor Field-Effect Transistors), which use an electric field rather than current to control switching. MOSFETs are far more energy-efficient. As transistor dimensions shrank below 20nm, traditional planar MOSFETs began leaking current even when "off." Intel's solution in 2011 was the FinFET โ€” a 3D transistor where the conducting channel is shaped like a vertical fin rather than lying flat. This dramatically reduced leakage and allowed Moore's Law to continue for another decade.

Recommended Reading

Rate this chapter
4.7  / 5  (109 ratings)

๐Ÿ’ฌ Comments