CodeRigor

LESSON

Perceptrons, Boolean logic and depth

Threshold units as logic gates, why XOR needs a hidden layer, and a construction that turns any truth table into a two-layer network.

A neural network is built from small units, and an early model of a unit is a threshold gate. This lesson asks what such units can compute when wired together, for Boolean functions, where the answer is exact. This lesson pairs with Lectures 1 and 2 of CMU's 11-785 (Fall 2026), linked under Watch & read. You build a function that turns any truth table into a two-layer network, and you test it on every possible input.

A threshold unit computes z = w · x + b, where w holds one weight per input and b is the bias, and it outputs step(z): 1 when z ≥ 0, otherwise 0. With inputs that are 0 or 1, a few weight choices give logic gates:

  • AND: weights (1, 1), bias -2. The sum x₀ + x₁ - 2 reaches 0 only for (1, 1).
  • OR: weights (1, 1), bias -1. One active input is enough.
  • NOT: weight -1, bias 0. An input of 0 gives z = 0 and fires; an input of 1 gives z = -1 and stays off.
Python
import itertools

def unit(w, b, x):
    return int(sum(wi * xi for wi, xi in zip(w, x)) + b >= 0)

inputs = [(0, 0), (0, 1), (1, 0), (1, 1)]
print("AND", [unit((1, 1), -2, x) for x in inputs])
print("OR ", [unit((1, 1), -1, x) for x in inputs])
print("NOT", [unit((-1,), 0, (v,)) for v in (0, 1)])

grid = [v / 2 for v in range(-6, 7)]   # -3.0 to 3.0 in steps of 0.5
xor = [0, 1, 1, 0]
hits = sum(
    [unit((w0, w1), b, x) for x in inputs] == xor
    for w0, w1, b in itertools.product(grid, repeat=3)
)
print("settings tried:", len(grid) ** 3, "that give XOR:", hits)

The last lines try 2197 settings for XOR and find none. The reason is short. XOR of (0, 0) is 0, so b < 0. XOR of (1, 0) and of (0, 1) is 1, so w₀ + b ≥ 0 and w₁ + b ≥ 0. Add those two: w₀ + w₁ + 2b ≥ 0, so w₀ + w₁ + b ≥ -b > 0, and the unit would also fire on (1, 1). Geometrically, a unit fires on one side of a line, and XOR's two true corners lie on one diagonal of the square, its false corners on the other.

A hidden layer removes the limit. Give XOR one hidden unit per input that should output 1: h₁ fires only on (0, 1), h₂ only on (1, 0), and an output unit takes the OR of the two.

XOR with one hidden layer
start / 4
Edge labels are weights. Each hidden unit fires on exactly one input, and the output unit ORs them.
XOR with one hidden layer
Edge labels are weights. Each hidden unit fires on exactly one input, and the output unit ORs them.

The same idea works for any Boolean function of n inputs. For every row where the function is 1, add one hidden unit that fires on that row alone: weight +1 where the row has a 1, weight -1 where it has a 0, and bias equal to minus the number of 1 bits in the row. The output unit is an OR, with all weights 1 and bias -1. That is the sense in which one hidden layer can represent any Boolean function.

Why the unit fires on its row only: write z = (sum of the inputs at the row's 1 positions) - (sum of the inputs at its 0 positions) - (the row's number of 1 bits). The first sum is at most the row's number of 1 bits, and it equals that only when every one of those inputs is on. The second sum is at least 0, and it equals 0 only when all the other inputs are off. So z ≤ 0 everywhere, and z = 0 exactly when the input is the row.

Majority of three inputs: four true rows, four hidden units
start / 4
Row i holds the bits of i, most significant first. Each marked row becomes one hidden unit.
Majority of three inputs: four true rows, four hidden units
Row i holds the bits of i, most significant first. Each marked row becomes one hidden unit.

Common mistakes:

  • Weight 0 on the zero bits. The unit for row 01 then reads only x₁ and also fires on 11. The -1 is what punishes an extra 1.
  • A bias that is off by one. With -(ones) + 1 the unit fires on neighbouring inputs too, and with -(ones) - 1 it never fires.
  • Making a unit for every row. The false rows need no unit, because the output only has to OR the true ones.

The price is the size. This construction needs one hidden unit per true row, up to 2ⁿ. Parity of n bits (1 when an odd number of inputs are on) has 2ⁿ⁻¹ true rows, so the network doubles with every extra input. Do not read that as a law for every one-hidden-layer network: with threshold units, n hidden units suffice. Unit k fires when at least k inputs are on, and the output unit uses weights +1, -1, +1, ... and bias -1. The snippet below checks both on every input.

Python
import numpy as np

def step(z):
    return (z >= 0).astype(int)

def bits(i, n):
    return np.array([(i >> (n - 1 - k)) & 1 for k in range(n)])

for n in range(2, 7):
    X = np.array([bits(i, n) for i in range(2 ** n)])
    want = X.sum(axis=1) % 2
    rows = [i for i in range(2 ** n) if bin(i).count("1") % 2]
    W1 = np.array([2 * bits(i, n) - 1 for i in rows])
    b1 = np.array([-bits(i, n).sum() for i in rows])
    by_table = step(step(X @ W1.T + b1) @ np.ones(len(rows)) - 1)
    V1, c1 = np.ones((n, n)), -np.arange(1, n + 1)
    w2 = np.array([(-1) ** k for k in range(n)])
    by_count = step(step(X @ V1.T + c1) @ w2 - 1)
    ok = (by_table == want).all() and (by_count == want).all()
    print(f"n={n}: table {len(rows)} hidden units, counting {n}, both right: {ok}")

Depth is the other way to pay less. XOR costs three units, so a chain of n - 1 XOR blocks computes parity with 3(n - 1) units when a later block may read an original input directly. In a strictly layered network every input that is still waiting needs a pass-through unit per layer (weight 1, bias -1 copies a 0 or 1), which brings the total to n² - 1 units (built and tested for n = 2 to 8). At n = 8 the truth-table network has 128 hidden units and 129 in all, against 21 units for the direct chain and 63 for the layered one.

The tests run each network on all 2ⁿ inputs and compare with the table, which is an exact check for these sizes. Hidden tests also confirm that each hidden unit fires on one row only, and that threshold_network handles deeper networks. None of this says how a network would find such weights. A step has zero slope almost everywhere, so gradient descent cannot train it, and the construction is a statement about what can be represented, not about what training will reach. The size claims describe this construction and these chains, not the smallest possible network.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →