← Runtimes
Runtimes

Booleans and logic gates

Can the checker approve this controller configuration?

Use a complete Boolean model to check a controller configuration.

Outcome

Compare complete gate truth tables, follow intermediate signals and preserve a Boolean rule when its gate network changes.

Terminology (4)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Truth table
The output for every possible combination of the stated Boolean inputs.
Combinational network
A gate network whose outputs depend on its present inputs, with no stored history.
NAND
NOT of AND: its output is false only when both inputs are true.
Fan-out
One signal feeding multiple destinations. The ideal model reuses its value; a physical device also has electrical load limits.

What a Boolean gate models

A Boolean value has two possibilities, false and true, written here as 0 and 1. A logic gate applies a fixed rule to its inputs to produce an output. Its truth table lists that output for every combination of those inputs. Two gates can agree on an example while following different rules overall.

Connecting one gate’s output to another gate’s input forms a network. In the ideal combinational model used here, the outputs depend only on the present inputs; there is no remembered state. We can compare that behavior without claiming anything about the electrical behavior or timing of a physical controller.

01The row your test missed

One input pair can hide the wrong gate

You maintain a lighting-controller configuration tool. Its preview passes one switch test, but installers report that the light sometimes ignores a switch change. Before exporting another configuration, find the missing input case and check what the preview can actually prove.

The configuration represents two maintained switch positions, a and b. Either switch must change the light when flipped; these are not momentary buttons. The preview currently uses OR. Its test a=0, b=1 passes, but an XOR candidate passes that test too. Choose a test that can tell which rule satisfies the requirement.

The test a=0, b=1 passes for both OR and XOR. Which single additional input row distinguishes them?

02Does ^ produce a Boolean?

The same symbol does not promise the same type

The preview is implemented in ordinary source code. Porting its Boolean comparison between languages introduces a second question: does the operator accept Boolean operands and produce a Boolean result? Predict this probe under its shown check/run command.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const a = true;
const b = false;

const different: boolean = a ^ b;

console.log(different);
terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
npx tsc main.ts --noEmit --skipLibCheck && node main.ts
Using the shown check/run command, what happens with this exact source?

For two actual Boolean values, inequality has the XOR table in all four languages. The lazy operators && and || can skip their right operand; a drawn gate table makes no such claim about evaluating source expressions. Keep input observations separate from the pure Boolean combination.

03One signal, two destinations

A correct gate can still be in the wrong place

The installer now needs a maintenance hold that keeps the light off, plus a diagnostic monitor that still shows whether the switches disagree. The preview must expose both outputs. different reports the original positions; light follows different when hold=0 and stays dark when hold=1. Suppressing the inputs during hold would hide useful diagnostic information.

Preview requirements for the maintenance-hold configuration
abholddifferentlight
00000
00100
01011
01110
10011
10110
11000
11100

Trace named intermediate signals to each destination. A change before a fan-out can affect both destinations; a change on one branch affects only that branch. Compare every external input row after a rewiring, including the rows where the two outputs intentionally differ.

A combinational network has no stored history. A push button that toggles a remembered light state needs another model: the same released inputs can then require two different outputs. That behavior cannot be specified by this present-input table alone.

04Same table, different cells

A different gate inventory can preserve the rule

The tool must also check configurations for a compact export profile. In this lesson, that supplied profile accepts only NAND records and has seven record slots. A configuration with correct output bits can still be rejected because it cannot be encoded in that profile. This is an authored format constraint, not a vendor specification.

NAND produces NOT of AND. Its output is 1 for 00, 01 and 10, and 0 for 11. Joining its inputs gives inversion: NAND(x,x) is NOT x. Invert the output of a NAND to obtain AND. Invert both inputs to a NAND to obtain OR.

Start with one small conversion: this complete program builds OR from three NAND operations. Compare its four output rows with the OR column above. The expression changes shape while the Boolean function stays the same.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
function nand(a: boolean, b: boolean): boolean {
  return !(a && b);
}

function either(a: boolean, b: boolean): boolean {
  const notA = nand(a, a);
  const notB = nand(b, b);

  return nand(notA, notB);
}

for (const a of [false, true]) {
  for (const b of [false, true]) {
    console.log(Number(a), Number(b), Number(either(a, b)));
  }
}

NAND can therefore build networks made from AND, OR and NOT, including a two-input XOR. Equivalent constructions need not allocate the same number of modeled cells. Reuse one intermediate signal at multiple destinations instead of assuming a repeated expression shares hardware.

The checker uses the supplied GateBoard as its fixed-network model. Wire identifies a signal and each GateBoard operation allocates one cell, corresponding to one export record. Drawing two connections from the same returned Wire reuses that signal. A second gate call creates a second cell, even if its inputs match an earlier call. The network is constructed once; the reader then drives its three external input wires through the rows. The compact profile checks those records before export. The lab does not serialize a file or connect to a device, and a gate call says nothing about compiler instruction counts.

05Check the new configuration

A force-on setting changes the preview requirements

A new configuration needs a force-on setting for installation checks. Unlike maintenance hold, force=1 must turn the light on, even when the switches agree. The disagreement monitor must still report the original switch positions. Restore the base preview, add that setting, then make the same behavior fit the compact profile. The selected language runs the supplied ideal model; Wire, GateBoard, row decoding and observations are supplied.

Validate a controller configuration

Check the full output table, then satisfy the compact profile without changing those outputs.

  1. easy · R-GATES-V4-1 Restore the preview table
  2. medium · R-GATES-V4-2 Add the force-on setting
  3. hard · R-GATES-V4-3 Export the NAND-only profile
Check this idea later
Two pure Boolean implementations agree on three of four two-input rows. What can you conclude?
The checker matches every input row and reports seven supported NAND records. Which claim is justified?
Sources

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.