logic gates

We’ve previously used a turn turn to reverse the direction of a marble. But turns serve another key function:

00/0000/00
loading…

and gate

The cyan cell only gets a marble if we input both the left marble and the right marble.

tip

This is similar to the switch switch, except we allow the signal marble to fall all the way into the checker cell if it’s unoccupied.

Because this cell needs both one input and the other, it’s called an AND gate, which is a particular type of logic gate.

logic gates

A logic gate is a device with these properties:

  • It has a fixed number of inputs and outputs
  • Each input and output is always either 0 or 1
  • Each output is determined by applying a rule to the inputs

For example, our AND gate has 2 inputs and 1 output. The rule for the output is:

  • 1 if the inputs are 1 and 1
  • 0 otherwise

The standard way of describing a logic gate is with its truth table, where we show what output is associated with each combination of inputs:

Input A
Input B
01
000
101

To find out the output for any given combination of inputs, look at the value in the corresponding cell.

For compactness, we’ll write our truth tables like this: . The filled-in cells correspond to a 1; the empty cells are 0. For example:

  • means “always output 0, regardless of input”
  • means “always output 1, regardless of input”
  • means “copy input A, ignore input B”
  • means “copy input B, ignore input A”
  • means “only output 1 if both inputs were 1” (AND Gate)

This will help us recognise when we encounter the same truth table again later.

definition

A logic gate transforms inputs to an output based on a rule.
An AND gate outputs 1 only if both inputs were 1.
A truth table summarises how a particular logic gate behaves.

why does this matter?

Remember our read-transform-output loop? To build a computer, we need to be able to transform the binary representation of data.

A logic gate transforms individual bits of data. By combining them together in clever ways, we can make them transform any binary data into new data.

Think of it like cooking a meal. You can break down any recipe into tiny steps:

stepinstruction
1Pick up an onion with your right hand
2Put the onion on a chopping board
3Pick up a knife with your left hand
4Put the knife against the onion
2,046Put your right hand on the oven dial
2,047Turn your right hand 30 degrees
3,067Put plate on table
3,068Let go of plate

Even though each step is simple, we can combine them to make any kind of dish. It’s the same with logic gates — each one may be simple, but by combining them together, we can make them transform any kind of data.

next

In the next few tutorials, we’ll look at some other logic gates, and start to combine them into more interesting devices.

challenge

Our AND Gate outputs a 1 if both inputs are 1. Can you build a pattern that outputs a 1 if either input is a 1 (or both)? i.e. satisfy the truth table .

continue