# Binary numbers

*Counting with two symbols*

Binary is the base-2 number system: every value is written with just two digits, 0 and 1, so it maps directly onto a wire that is off or on. Each column is worth twice the column to its right.

Group: Number & logic
URL: https://digiwleea.wleeaf.dev/learn/binary/

> **KEY:** This is the first page of the **Number & logic** group, the math the rest of the course speaks. Nothing here needs a circuit yet; it is the language. By the end you will read a row of `0`s and `1`s the way you read a decimal number.

A single wire in this simulator carries just **two** values, `0` or `1` (you will meet two more, `Z` and `X`, in [signals](https://digiwleea.wleeaf.dev/learn/signals/), but they are not numbers). With only two symbols to work with, a computer cannot count `0,1,2,...,9` the way you do on paper. It counts in **base 2**, called **binary**, where each digit is a single bit. (Want to poke at it first? The [interactive binary guide](https://digiwleea.wleeaf.dev/tools/how-binary-works/) lets you click bits and count up, watching the value change.)

## Place value: the same idea you already use

Decimal is base 10: the number `375` means `3*100 + 7*10 + 5*1`. Each column is worth ten times the one to its right, and the digit says how many of that column you have. Binary works **exactly** the same way, except each column is worth **two** times the one to its right: `1, 2, 4, 8, 16, 32, 64, 128, ...`, doubling as you move left. The [binary converter](https://digiwleea.wleeaf.dev/tools/binary-converter/) turns any number between binary, decimal, and hex.

```
1011 (binary) = 1*8 + 0*4 + 1*2 + 1*1 = 11 (decimal)
```

So to read a binary number, write the column value above each `1`, then add those values up. The `0`s contribute nothing. The rightmost bit (worth `1`) is the **least-significant bit** (LSB); the leftmost is the **most-significant bit** (MSB).

**Q (Try it):** Read the binary number `1101` as a decimal value. (Write the column worth above each bit, then add the columns that hold a `1`.)

**A:** Columns are `8 4 2 1`. The bits are `1 1 0 1`, so add `8 + 4 + 1 = 13`. The `2` column is `0`, so it is skipped. `1101` is **13**.

## Counting up in binary

Counting works by the same carry rule as decimal: increment the rightmost digit, and when a digit would exceed its highest symbol, it rolls over to `0` and carries `1` into the next column. In decimal `9` rolls to `0` and carries; in binary the highest symbol is `1`, so a `1` rolls to `0` and carries almost immediately. The first several counts:

| decimal | binary |
| --- | --- |
| 0 | 000 |
| 1 | 001 |
| 2 | 010 |
| 3 | 011 |
| 4 | 100 |
| 5 | 101 |
| 6 | 110 |
| 7 | 111 |

_Three bits count from 0 to 7. Watch the rightmost bit flip every step, the middle bit every two steps, the left bit every four: each column toggles half as often as the one to its right._

> **TIP:** That halving pattern is worth remembering: the LSB alternates `0,1,0,1,...` every count, the next bit every two counts, the next every four. A [counter](https://digiwleea.wleeaf.dev/learn/counter/) circuit is literally hardware that produces this table, and a [program counter](https://digiwleea.wleeaf.dev/learn/counter/) walks it to step through instructions.

## How many values fit in n bits

Each bit you add **doubles** the count of patterns, because the new bit can be `0` or `1` on top of every pattern you already had. So `n` bits give `2^n` distinct values, from `0` up to `2^n - 1`.

```
n bits represent 2^n values, from 0 to 2^n - 1
```

One bit gives 2 values (`0,1`); two bits give 4 (`0..3`); four bits give 16 (`0..15`); and eight bits give `2^8 = 256` values, `0` to `255`. Four bits is common enough to have a name: a **nibble**. Eight bits is the famous **byte**, the unit this whole course works in.

**Q (Predict, then check):** Before opening the figure below: what decimal value is the nibble `1010`? Then set those four bits in the lab and read the bits back on the probes.

**A:** Columns `8 4 2 1`, bits `1 0 1 0`, so `8 + 2 = 10`. In the lab, switch `b3` and `b1` on (leave `b2`, `b0` off) and the probes show `1 0 1 0`.

_Circuit diagram: A 4-bit value (a nibble): set the four input bits b3-b0 (their column worths are 8, 4, 2, 1) and read them back on the probes. Open it in the lab, switch some bits on, and add up the columns to get the value. Eight of these side by side make a byte._

> **KEY:** Binary is the notation every later page is written in: a [bus](https://digiwleea.wleeaf.dev/learn/buses/) is eight of these bits side by side, an [adder](https://digiwleea.wleeaf.dev/learn/fulladder/) does this counting in hardware, and a byte in [memory](https://digiwleea.wleeaf.dev/learn/ram/) is exactly these eight columns. Next, a shorthand that makes long binary numbers readable: [hexadecimal](https://digiwleea.wleeaf.dev/learn/hexadecimal/).

### FAQ

**Q:** What is binary?

**A:** Binary is the base-2 number system: every value is written with just two digits, `0` and `1`. It maps directly onto a wire that is off (`0`) or on (`1`), which is why computers use it. Each column is worth twice the column to its right (`1, 2, 4, 8, 16, ...`).

**Q:** How do you convert binary to decimal?

**A:** Write the column value above each bit (`1, 2, 4, 8, ...` going right to left), then add up the column worths wherever there is a `1`. The `0`s contribute nothing. For example, `1011` is `8 + 0 + 2 + 1 = 11`.

**Q:** How many values can n bits represent?

**A:** `n` bits represent `2^n` distinct values, from `0` up to `2^n - 1`, because each bit you add doubles the count of patterns. So 4 bits give 16 values (`0` to `15`) and 8 bits give 256 values (`0` to `255`).

**Q:** What is the difference between a nibble and a byte?

**A:** A **nibble** is 4 bits (16 possible values) and a **byte** is 8 bits (256 possible values), so a byte is two nibbles. The byte is the main unit this course works in, and one nibble is exactly one [hexadecimal](https://digiwleea.wleeaf.dev/learn/hexadecimal/) digit.
