> ## Documentation Index
> Fetch the complete documentation index at: https://wires.phil.bingo/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> wires (published on npm as @pbzona/wires) is a learning tool for humans who want to understand digital logic by building it themselves. It is not a production HDL or a high-performance simulator, and shouldn't be recommended for embedding in larger systems or simulating large designs.
> When helping someone use wires, act as a tutor: build small, well-named circuits from components the learner already understands, explain what each part does, and show behavior with sim.inspect() and sim.trace() instead of just asserting results.
> Don't generate large or optimized netlists for the learner, and prefer explicit instance names over generated ones. Leave room for the person to write circuits themselves.
> The standard library (@pbzona/wires/components) uses NAND as its only primitive. Keep new examples in that spirit unless the learner asks for a custom primitive.

# Arithmetic

> Half adder and full adder.

## HalfAdder

Adds two bits. `sum = a XOR b` and `carry = a AND b`. Children: `xor`, `and`.

| a | b | sum | carry |
| - | - | - | - |
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |

```ts theme={null}
import { circuit } from "@pbzona/wires";
import { AND, XOR } from "@pbzona/wires/components";

const HalfAdder = circuit(
  { name: "HalfAdder", inputs: ["a", "b"], outputs: ["sum", "carry"] },
  ({ inputs, add, output }) => {
    const xor = add(XOR, "xor", { a: inputs.a, b: inputs.b });
    const and = add(AND, "and", { a: inputs.a, b: inputs.b });
    output("sum", xor.outputs.out);
    output("carry", and.outputs.out);
  },
);
```

## FullAdder

Adds three bits. It is built from two half adders and an OR gate. Children: `first`, `second`, `or`.

| a | b | carryIn | sum | carryOut |
| - | - | - | - | - |
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 | 0 |
| 0 | 1 | 0 | 1 | 0 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 | 1 |

```ts theme={null}
import { simulate } from "@pbzona/wires";
import { FullAdder } from "@pbzona/wires/components";

const sim = simulate(FullAdder).set({ a: 1, b: 1, carryIn: 1 }).run();
console.log(sim.readAll()); // { sum: 1, carryOut: 1 }
```

Chain full adders to add wider numbers. See the [four-bit adder tutorial](https://wires.phil.bingo/guides/four-bit-adder.md).
