🚀 Alguni Start learning

Unit 4: Numbers

Counting with nothing but on and off.

Unit 4 of 8 in Digital logic for kids. Its 5 lessons are Counting on Two Fingers, Eight Four Two One, Asking a Number Questions, Below Zero and Reading the Wires — below is everything each one explains, and a question or two from it to try.

Every chip on this page was wired up and run on the gate-level simulator before it shipped, and prints exactly the table shown.

This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.

🔢 Counting on Two Fingers

You already know how this works

In the number 307, the 3 is not really three. It is three *hundreds*. Each place is worth ten times the one to its right: 1, 10, 100, 1000.

Wires have no ten. They only have on and off. So each place is worth two times the one to its right: 1, 2, 4, 8.

That is the whole idea. Nothing else changes.

Try it yourself

Counting up in binary: 0, 1, then what?

  • 10
  • 2
  • 11
  • 01

How many different numbers can four wires hold?

  • 16
  • 8
  • 4
  • 15

Answer them in the app

🎚️ Eight Four Two One

Reading four wires as a number

Our four wires are called i3, i2, i1, i0. They are worth 8, 4, 2 and 1.

To read the number, add up the ones that are on:

1011 → 8 + 0 + 2 + 1 = 11

i0 is the ones column, so it goes on the right — exactly like the units column in a normal number.

Why only four?

Four wires is not what a real computer uses — yours has 64 in most places. We use four here because a table with 16 rows fits on a screen and a table with 18 million million million rows does not.

Everything you build works exactly the same at 64 wires. There is just more of it.

Try it yourself

What number is 0110?

  • 6
  • 3
  • 110
  • 9

How do you write 9 with four wires?

  • 1001
  • 0110
  • 1100
  • 1010

Answer them in the app

❓ Asking a Number Questions

Odd or even, for free

Is the number odd? Look at i0. That is the entire answer — the other three wires are all worth even amounts, so they cannot change it.

One snag: a wire has to get its value from a part. You cannot write out=i0 on its own. So we use an Or with the same wire on both sides, which is a plain copy.

HDL

CHIP IsOdd {
    IN i3, i2, i1, i0;
    OUT out;
    PARTS:
    Or(a=i0, b=i0, out=out);
}

USE IsOdd;
SET i3=0, i2=1, i1=1, i0=0;
SHOW out;
SET i0=1;
SHOW out;

It prints

out=0
out=1

Is it zero?

A number is zero only when every wire is off. So Or the wires together to ask "is anything on at all?", then flip the answer.

This chip turns up again in unit 6, where a CPU uses it to decide whether to jump.

HDL

CHIP AllZero {
    IN i3, i2, i1, i0;
    OUT out;
    PARTS:
    Or(a=i3, b=i2, out=t1);
    Or(a=i1, b=i0, out=t2);
    Or(a=t1, b=t2, out=any);
    Not(in=any, out=out);
}

USE AllZero;
SET i3=0, i2=0, i1=0, i0=0;
SHOW out;
SET i1=1;
SHOW out;

It prints

out=1
out=0

Try it yourself

Why can the other three wires never make a number odd?

  • They are worth 8, 4 and 2, and every one of those is even
  • They are always 0
  • Because i0 is bigger than them
  • They can — the chip is wrong

Answer it in the app

➖ Below Zero

There is no minus sign

A wire is on or off. There is nowhere to put a minus sign.

So computers use a trick. With four wires, counting past 15 wraps back round to 0 — like a clock. And if 15 + 1 is 0, then 15 behaves exactly like −1.

14 is −2. 13 is −3. The top half of the numbers is quietly the negative half.

How to make a number negative

To turn a number into its negative: flip every wire, then add 1.

Here it is on 3 (0011). Flipping gives 1100, and adding one gives 1101 — which is 13, and 13 is how −3 looks in four wires.

Check it: 3 + 13 = 16, which wraps round to 0. That is what a negative is *for*.

HDL

CHIP Negate4 {
    IN i3, i2, i1, i0;
    OUT o3, o2, o1, o0;
    PARTS:
    Not4(i3=i3, i2=i2, i1=i1, i0=i0, o3=f3, o2=f2, o1=f1, o0=f0);
    Inc4(i3=f3, i2=f2, i1=f1, i0=f0, o3=o3, o2=o2, o1=o1, o0=o0);
}

USE Negate4;
SET i3=0, i2=0, i1=1, i0=1;
SHOW;

It prints

o3=1 o2=1 o1=0 o0=1

Try it yourself

With four wires, which number acts like −1?

  • 15, because 15 + 1 wraps round to 0
  • 1
  • 0
  • 8

How can a chip tell at a glance that a number is negative?

  • The top wire is 1
  • The bottom wire is 1
  • All the wires are 1
  • It cannot tell

Answer them in the app

🏆 Reading the Wires

Try it yourself

What number is 1101?

  • 13
  • 11
  • 14
  • 1101

What does this print?

HDL

CHIP Top {
    IN i3, i2, i1, i0;
    OUT out;
    PARTS:
    Or(a=i3, b=i3, out=out);
}

USE Top;
SET i3=1, i2=0, i1=0, i0=1;
SHOW out;
SET i3=0;
SHOW out;

Answer them in the app