◆ Rebuild

Part 15 · Knowledge and Science

Logic and Binary

The arithmetic of true and false — AND, OR, NOT and truth tables — and of counting with only two digits, 0 and 1. It sharpens everyday reasoning, and it is exactly what a machine of switches needs to add numbers, store codes and, in the end, compute.

Difficulty
●●●●●
Time
An afternoon to learn binary counting; a week to be at home with truth tables and adders; a day to wire a switch demonstration
Steps from zero
2 prerequisites →

You need first

Arithmetic and Fractions

This unlocks

The ComputerElectronic CountersThe Relay ComputerCodes, Ciphers and Encryption
The full path from nothing — 2 steps before this one
  1. Writing and Numbers
  2. Arithmetic and Fractions
  3. Logic and Binary

Do them roughly in this order. See it on the tech tree →

Why it matters

Two quite different needs meet in this chapter.

The first is reasoning well. Every experiment (The Scientific Method), every diagnosis (Examining a Patient) and every repair depends on drawing correct conclusions from facts. Logic is the set of rules that tells you which conclusions really follow and which only seem to.

The second is making machines think. A switch is either on or off. A relay is either pulled in or released. A hole in a card is either punched or not (see Punched-Card Tabulating Machines). Anything with two states can hold a 0 or a 1, a "false" or a "true". The 19th-century discovery that logic can be written as arithmetic, and that arithmetic can be done with only 0 and 1, means that a large enough heap of simple switches can add, compare, remember and follow instructions. That is the road from the The Telegraph through the The Relay Computer to the The Computer and Programming.

You need only the arithmetic of Arithmetic and Fractions to follow it. To build the circuits you need a The Battery, Copper Wire and a small The Light Bulb or other indicator — but you can learn everything first with pebbles and paper.

How it works

A statement is something that is either true or false: "the kiln is hot", "the gate is open", "this number is even". Write true as 1 and false as 0.

Three operations combine statements:

  • AND is true only when both parts are true. "The furnace is hot AND the bellows are running."
  • OR is true when at least one part is true. "It is raining OR the ground is wet from the river."
  • NOT turns true into false and false into true.

A truth table lists every possible combination of inputs and the result. With two inputs there are only four rows, so you can check a rule completely — something you can rarely do anywhere else in life.

ABABout000010100111ANDbothABABout000011101111ORat least oneAAout0110the oppositeNOTABABout000011101110XORexactly one
Fig. 1 The four basic logic operations, with the symbols engineers draw for them and their complete truth tables. 1 means true (or on); 0 means false (or off).

A fourth operation, XOR ("exclusive or"), is true when exactly one input is true — "one or the other, but not both". It is the heart of binary addition (below).

What you need

Item Use
Slate or paper Truth tables and binary sums
Pebbles, or a row of cups Binary place-value counting (a cup is 1 if it holds a pebble)
For the switch demonstration: a The Battery of 2–3 cells, a small The Light Bulb (or a galvanometer or a buzzer), insulated Copper Wire, a wooden board, copper or brass strip, nails Building AND and OR from switches

Logic for clear thinking

Many arguments take the form "If A, then B." Two ways of reasoning from it are valid (always correct when the starting facts are true) and two are traps:

Form Example Valid?
If A then B. A is true. So B. If the furnace reached 1,100 °C, the slag ran. It reached 1,100 °C. So the slag ran. Yes
If A then B. B is false. So A is false. The slag did not run. So the furnace did not reach 1,100 °C. Yes
If A then B. B is true. So A. The slag ran. So the furnace reached 1,100 °C. No — something else (extra flux) may have made it run.
If A then B. A is false. So B is false. It did not reach 1,100 °C. So the slag did not run. No — same reason.

The two traps are behind a great many false beliefs: "People with cholera have diarrhoea; she has diarrhoea; so she has cholera." Watch for them in your own reasoning.

De Morgan's rules tell you how NOT passes through AND and OR:

  • NOT (A AND B) = (NOT A) OR (NOT B). "It is not true that the gate is open and unguarded" means "the gate is closed, or it is guarded."
  • NOT (A OR B) = (NOT A) AND (NOT B). "Neither rain nor river" means "no rain, and no river."

These are useful in writing clear rules, and essential in designing circuits.

Logic with switches

+−batteryABlampAND — switches in seriesThe lamp lights only if A and Bare both pressed closed.+−batteryABlampOR — switches side by sideThe lamp lights if A or B(or both) is pressed closed.
Fig. 2 Left: two switches in a row (series) make AND — the lamp lights only if both are closed. Right: two switches side by side (parallel) make OR — either one lights the lamp.

Building the demonstration

  1. Make switches. Nail a strip of springy copper or brass, about 6 cm × 1 cm, to a board at one end so the free end stands a few millimetres above a second nail head. Pressing the strip down touches the nail and closes the circuit; letting go opens it. Attach a wire to the strip's fixed end and one to the contact nail. (See Copper Wire for making wire and Sheet Metal and Rolling for strip.)
  2. AND: wire battery → switch A → switch B → lamp → back to battery. The lamp lights only when both are pressed.
  3. OR: wire both switches side by side between the battery and the lamp. Either one lights it.
  4. NOT needs something that opens when it is pushed. Make a switch that rests closed and is pushed open — or, much more useful, a relay: an The Electromagnet that pulls a springy iron strip away from a contact when current flows. Current in → lamp out. Relays let one circuit control another, so outputs can feed inputs, and that is how whole calculating machines are built (see The Relay Computer).

Tip One gate builds everything. A NAND gate (AND followed by NOT) can be combined to make AND, OR, NOT and every other gate. If you can make one reliable kind of NAND — a relay, later a The Vacuum Tube or a transistor (Semiconductors and the Transistor) — you can make a whole computer from copies of it.

Binary numbers

The numbers in Writing and Numbers use ten digits, and each place is worth ten times the place to its right: ones, tens, hundreds. Binary uses only two digits, 0 and 1, and each place is worth two times the place to its right:

Place value 128 64 32 16 8 4 2 1
13 0 0 0 0 1 1 0 1
45 0 0 1 0 1 1 0 1
200 1 1 0 0 1 0 0 0

So 13 is written 1101: one 8, one 4, no 2, one 1. Each binary digit is called a bit. A group of 8 bits — enough for any number from 0 to 255 — is a byte.

Binary to ordinary numbers: add the place values where there is a 1. 101101 = 32 + 8 + 4 + 1 = 45.

Ordinary to binary: keep halving, writing down the remainder (0 or 1) each time; then read the remainders from the last to the first.

200 ÷ 2 = 100 r 0 100 ÷ 2 = 50 r 0 50 ÷ 2 = 25 r 0 25 ÷ 2 = 12 r 1
12 ÷ 2 = 6 r 0 6 ÷ 2 = 3 r 0 3 ÷ 2 = 1 r 1 1 ÷ 2 = 0 r 1

Reading upward from the last: 11001000 = 128 + 64 + 8 = 200. Correct.

Counting: 0, 1, 10, 11, 100, 101, 110, 111, 1000… Exactly like ordinary counting, but you "carry" after 1 instead of after 9.

Counting on your fingers: let your thumb be 1, index 2, middle 4, ring 8, little finger 16. A finger raised is a 1. One hand counts from 0 to 31; two hands to 1,023. It is a good way for children to feel how binary works.

Shorthand. Long rows of bits are hard to read. Group them in fours from the right and write each group as one symbol from 0–9 and A–F (A = 10 … F = 15). This is hexadecimal: 1100 1000 = C8. Engineers use it to write binary compactly.

Binary arithmetic

Addition has only four facts: 0 + 0 = 0; 0 + 1 = 1; 1 + 1 = 10 (write 0, carry 1); 1 + 1 + 1 = 11 (write 1, carry 1).

Column addition168421111carries1011+011010001= 11= 6= 17Half adderABXORANDsumcarryFour full adders in a row add two 4-bit numbersfull adderA3B3S3full adderA2B2S2full adderA1B1S1full adderA0B0S00S4carryEach adder passes its carry to the one on its left, like carrying on paper.
Fig. 3 Adding 11 + 6 in binary, column by column, with carries. Below: a half adder — an XOR gate gives the sum bit and an AND gate gives the carry. Chain full adders side by side, each passing its carry to the left, and you can add numbers of any length.

Worked example: 1011 (11) + 0110 (6).

  1. Rightmost column: 1 + 0 = 1. Write 1.
  2. Next: 1 + 1 = 10. Write 0, carry 1.
  3. Next: 0 + 1 + carry 1 = 10. Write 0, carry 1.
  4. Next: 1 + 0 + carry 1 = 10. Write 0, carry 1.
  5. The final carry becomes a new place: 10001 = 16 + 1 = 17. Correct.

Subtraction works with borrowing, as in ordinary numbers: 10001 − 0110 = 1011 (17 − 6 = 11). Machines usually subtract by adding a special form of the negative number (the "two's complement": flip every bit and add 1), so that one adding circuit does both jobs.

Multiplication is easier than in ordinary numbers, because the only multiplication facts are × 0 and × 1. Shift and add: 101 × 11 = 101 (× 1) + 1010 (× 10, shifted one place) = 1111 (5 × 3 = 15). Shifting a binary number one place left doubles it, just as shifting an ordinary number multiplies it by ten.

From logic to arithmetic: the adder

Look at the addition facts again and compare them with the truth tables. Adding two single bits A and B gives:

A B Sum bit Carry
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1

The sum column is exactly A XOR B. The carry column is exactly A AND B. So an XOR gate and an AND gate together — a half adder — add two bits. A full adder also takes in a carry from the column to its right; it is made from two half adders and an OR gate. Put four full adders in a row, each passing its carry to the next, and you have a machine that adds any two numbers from 0 to 15. Eight in a row add numbers up to 255. This is the core of every calculating machine since the 1930s.

Codes: letters as numbers

Once numbers are bits, anything can be. Number the letters A = 1, B = 2 … Z = 26; five bits (0–31) are enough for the alphabet with a few spare for space and punctuation. The French telegraph engineer Émile Baudot used exactly such a 5-bit code in the 1870s, sending letters as patterns of on and off pulses (see The Telegraph). Seven or eight bits allow capitals, small letters, digits and signs.

Catching errors. Add an extra parity bit to each code so that the number of 1s is always even. If a single bit gets flipped by noise on the line, the count becomes odd and the receiver knows the character is wrong and can ask for it again. Cleverer schemes can even find which bit is wrong and correct it.

How to tell it worked

  • Check every row. For any rule or circuit with two or three inputs, try all 4 or 8 combinations and compare with the truth table. With switches, press every combination and watch the lamp.
  • Convert back. Any binary sum can be checked by converting both inputs and the answer to ordinary numbers.
  • The finger test. Count to 31 on one hand without hesitation and you understand place value in binary.
  • De Morgan check. Build NOT (A AND B) and (NOT A) OR (NOT B) side by side; for all four input pairs they must agree.

Note Troubleshooting. Binary conversion gives a reversed number → you read the remainders top to bottom. Read them from the last remainder up. Binary sum off by a power of two → a carry was dropped. Write the carries above each column. Lamp lights dimly or flickers → dirty contacts. Scrape both surfaces bright; make the spring strip press firmly. Lamp lights with switches open → wires touching where they shouldn't. Insulate them (waxed thread wrapped round, or rubber). An argument "feels" right but leads somewhere wrong → write it as "If A then B" and check which of the four forms it is.

Dangers

  • Short circuits. Wiring a battery straight across with no lamp in the path makes the wire hot enough to burn skin and can damage the cells. Always include the lamp or another load. Never experiment with mains electricity from a The Generator; see Wiring and Electrical Safety.
  • Confident errors. A machine or a calculation built on a wrong truth table gives wrong answers with complete confidence. Test every new circuit against every case before you rely on it.

Going further

With gates and adders understood, you can build calculating machines from relays (The Relay Computer), later from vacuum tubes and transistors (Semiconductors and the Transistor, Integrated Circuits), up to the stored-program The Computer and Programming. Binary codes carry text over the The Telegraph, Radio and Networks and the Internet. For hand calculating with numbers, see Calculating Aids.

How it was first done

Aristotle set out the first system of logic, the syllogism, in Greece in the 4th century BC; the Stoic philosopher Chrysippus soon after studied "if… then", "and" and "or" between whole statements. In 1605 and 1623 Francis Bacon described a cipher that wrote each letter as a pattern of five A's and B's — a 5-bit binary code. Gottfried Leibniz published a full account of binary arithmetic in 1703, noticing its likeness to the hexagrams of the Chinese I Ching. George Boole, a self-taught English schoolmaster, showed in 1847 and 1854 that logic could be written and calculated as algebra, and Augustus De Morgan gave his rules at the same time. In 1869 William Stanley Jevons built a "logic piano" that solved logic problems mechanically. In 1937 Claude Shannon, a student in the United States, showed that relay circuits are Boolean algebra and can add binary numbers, and in 1941 Konrad Zuse in Berlin completed the Z3, a working programmable calculator built from about 2,600 relays and computing in binary.

Source: content/entries/knowledge/logic-and-binary.md