ViewWiki

The world, a little closer

Binary and logic gates.

Every computer is built from switches that are either on or off. How can a handful of on-off switches do arithmetic?

Explore the scene Interactive article

01 / The experiment

Two switches. One sum.

Flip the input bits and watch which gates light, then rewire the same gates into an adder.

Still of the Binary and logic gates 3D scene, which explores: How can a handful of on-off switches add two numbers?
Interactive scene

Loading the interactive version…

The explanation below is available while the scene loads.

Each gate's output is set only by its inputs, and a lit trace means it carries a 1. The board settles at once; real gates take a few trillionths of a second.Real chips build gates from transistors, usually as NAND or NOR, and pack billions on one sliver of silicon. These gates are drawn large, in their standard symbol shapes.

02 / Three gates

Three rules build everything.

Each gate turns its inputs into one bit. The table lists every possible input.

Both
AND0 0 → 00 1 → 01 0 → 01 1 → 1

AND

Gives 1 only when every input is 1, like two switches wired one after the other.

Gives 1 when
Both inputs are 1
Rows giving 1
1 of 4
In the half adder
Makes the carry
Either
OR0 0 → 00 1 → 11 0 → 11 1 → 1

OR

Gives 1 when any input is 1, like two switches wired side by side.

Gives 1 when
At least one input is 1
Rows giving 1
3 of 4
In the half adder
Says “at least one”
Flip
NOT0 → 11 → 0

NOT

One input, flipped. Also called an inverter; the small circle marks the flip.

Gives 1 when
The input is 0
Rows giving 1
1 of 2
In the half adder
Turns AND into “not both”
The big idea

Join AND, OR and NOT and the circuit can add: the carry is A AND B, and the sum is 1 when exactly one input is 1. Every calculation a computer makes is built from gates like these.

1 + 1 = 10

On or off

A light switch has two states, and so does every part of a computer’s memory and logic: a wire is either at a high voltage or a low one. Call them 1 and 0 and each one is a bit, short for binary digit. One bit can only say yes or no, but a row of bits can stand for anything that can be counted: a number, a letter, the colour of a pixel.

The board in the experiment has two input switches, A and B. A trace that carries a 1 glows; a trace that carries a 0 stays copper.

Gates decide

A logic gate takes one or two bits and gives back one bit by a fixed rule. Three are enough to build any computer:

  • AND gives 1 only when both inputs are 1.
  • OR gives 1 when either input is 1, or both.
  • NOT has one input and flips it: 0 becomes 1, 1 becomes 0.

Each rule fits in a truth table, a list of every possible input with its output. With two inputs there are only four rows: 0 0, 0 1, 1 0 and 1 1. Those four rows are the four buttons of Input bits, which count from 0 to 3 in binary: the left digit is A, the right is B.

Try it: press 0 0 and only the NOT gate’s light is on. Press 1 1 and AND and OR both light while NOT goes dark.

Back to the experiment

Counting in twos

Ordinary numbers use ten digits, and each place is worth ten times the one to its right. Binary uses only 0 and 1, and each place is worth twice the one to its right: 1, 2, 4, 8 and so on. So 10 in binary means one two and no ones, which is two, and 11 means three.

Adding works as it does at school, with a carry. 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1, and 1 + 1 = 10: write 0, carry 1.

Gates that add

Under Wire a half adder, choose Half adder and the same gates rearrange, joined by one more AND. Look at the two outputs as you flip the switches:

  • The carry is 1 only when both inputs are 1. That is exactly AND.
  • The sum is 1 when exactly one input is 1. No single gate does that, but OR says “at least one”, and NOT AND says “not both”. Joined by a second AND, they give “at least one, but not both”.

That rule has its own name, exclusive or (XOR), and together with the carry it makes a half adder: two bits in, their sum as a two-digit binary number out. Chain adders so that each passes its carry to the next and you can add numbers of any length. The arithmetic unit inside a processor is built this way, from millions of gates.

What this model leaves out

The board shows logic, not electronics. In a real chip each gate is a few transistors, tiny switches turned on and off by a voltage, and the most common gate is NAND (NOT AND), from which all the others can be made. Signals take time to pass through a gate, a few trillionths of a second today, and clocked circuits wait for every gate to settle before reading an answer; here the board settles at once. A real adder also needs a full adder for every column after the first, one that takes three inputs (two bits and the carry from the column before). The glowing traces and moving beads show which wires carry a 1 and which way the logic flows; real current is not a stream of beads.

Reuse with credit to ViewWiki and a link to this page.

04 / Learn this in a course

Practise it, step by step.

These 6 lessons teach the topic with checked questions, in course order.

  1. Bits on switches Computer science · Bits and data
  2. Counting in base 2 Computer science · Bits and data
  3. AND, OR and NOT Computer science · Logic gates
  4. Truth tables Computer science · Logic gates
  5. A half adder Computer science · Logic gates
  6. A full adder and the processor Computer science · Logic gates