Binary to decimal
Every column's worth is 2 raised to its index, counting from 0 on the rightmost column: index 0 is worth 2^0 = 1, index 1 is worth 2^1 = 2, index 2 is worth 2^2 = 4, and so on. To turn binary into decimal, add up the worth of every column that holds a 1. Take 1011:
| Bit | 1 | 0 | 1 | 1 |
|---|
| Index | 3 | 2 | 1 | 0 |
| Worth (2^index) | 8 | 4 | 2 | 1 |
| Count it? | yes | no | yes | yes |
8 + 2 + 1 = 11. So 1011 in binary is 11 in decimal. A 1 earns its column's value, a 0 earns nothing. The same method works at any width: keep raising 2 to the next index as you move left.
Decimal to binary
Going the other way, work down from the biggest power of two that fits: index 3 (worth 8), then index 2 (worth 4), then index 1 (worth 2), then index 0 (worth 1). Convert 13:
- The biggest power of two that fits in 13 is 8. Use it, remainder 5. Bit for 8 is 1.
- 4 fits in 5. Use it, remainder 1. Bit for 4 is 1.
- 2 does not fit in 1. Bit for 2 is 0.
- 1 fits in 1. Use it, remainder 0. Bit for 1 is 1.
Reading the bits from 8 down to 1 gives 1101. Check it: 8 + 4 + 1 = 13.
Bits, bytes and width
Bits are grouped so often that the groups have names. Eight bits make a byte.
The width of a group is simply how many bits it has, and width decides how many values it can hold: n bits give (2 to the power n) values.
| Width | Distinct values |
|---|
| 4 bits | 16 |
| 8 bits | 256 |
| 32 bits | about 4.3 billion |
Every extra bit doubles the range. That is why hardware designers care so much about width.
Bits on real wires
On a chip a binary number is not an abstraction. It is a row of wires side by side, each held high or low at this exact instant.
Figure: Eight parallel copper traces on a circuit board labelled bit 0 to bit 7, carrying the value 10110010
- 8 wires carry one byte.
- Reading them left to right gives the bits of the number.
- Change what the wires carry and you change the value the hardware sees.
That is all a number is in hardware: a bundle of wires, each a clean 0 or 1. Everything the rest of this course builds, from adders to memories, is a clever way of moving and combining these bundles.