Binary
Binary is the number system that computers use to store and process data. It is a base-2 system, so it uses only two digits: 0 and 1. Denary, the number system you use every day, is base 10 and uses the ten digits 0 to 9.
Digital circuits use components that can reliably hold one of two stable states: a high voltage or a low voltage, which you can think of as on or off. These two states are easy to tell apart even when the voltage wobbles slightly, so the machine rarely misreads a value. The states are written as 1 and 0. If a circuit had to distinguish ten different voltage levels, a small change would be enough to turn one digit into another and the stored data would become unreliable.
A bit is a single binary digit: one 0 or one 1. Groups of bits are given their own names:
Before a computer can work on anything, it must be converted into binary. Numbers, text, sound, graphics and the program instructions themselves are all stored as patterns of bits, so a piece of music and a photograph are handled in the same way once they are in memory.
Denary place values grow by powers of 10 from right to left: 1, 10, 100 and so on. Binary place values grow by powers of 2 from right to left: 1, 2, 4, 8, 16, because every step to the left doubles the value. To read a pattern, add the place values wherever there is a 1 and ignore the places holding a 0.
| Place value | 8 | 4 | 2 | 1 |
|---|---|---|---|---|
| Bits | 1 | 0 | 1 | 1 |
| Value | 8 | 0 | 2 | 1 |
Adding the values gives 8 + 2 + 1 = 11, so the nibble 1011 is 11 in denary.
A bit is one binary digit, and a bit pattern is a row of bits. With n bits you can make 2 to the power of n different patterns, written 2 ** n. Each pattern is a separate state, so each one can be given a different meaning or value.
Two bits give four patterns: 00, 01, 10 and 11, so 2 ** 2 is 4. Five bits give 2 ** 5 = 32 patterns, and ten bits give 2 ** 10 = 1024. That is enough for every pupil in a very large school to have their own different ten-bit code.
Start with the four two-bit patterns and put a 0 on the front of each one: 000, 001, 010 and 011. Now repeat with a 1 on the front: 100, 101, 110 and 111. You now have eight three-bit patterns, twice as many as before, because every old pattern produced two new ones. Add a fourth bit and the count doubles again to 16.
The all-zero pattern counts as one of the states, so count every pattern, not just the ones with a 1 in them.
An unsigned binary pattern uses every bit for place value. No bit is set aside to signal a minus sign, so an unsigned pattern always stands for zero or a positive number.
A pattern's states are the different combinations of bits it can show. A 3-bit pattern has 8 states, running from 000 up to 111, and each state stands for one value. The first of those values is zero.
| Pattern | Value it stands for |
|---|---|
| 000 | 0 |
| 001 | 1 |
| 010 | 2 |
| 011 | 3 |
| 100 | 4 |
| 101 | 5 |
| 110 | 6 |
| 111 | 7 |
Because zero takes up one of the states, the largest value an unsigned pattern can hold is always one less than the number of states. Those 8 states reach only as far as 7. A 4-bit pattern has 16 states, so its top value is 15 — saying 16 would describe the number of states, not the largest value.
The most significant bit (MSB) is the leftmost bit of the pattern. It holds the biggest place value, so changing it alters the number far more than changing any other bit. The least significant bit (LSB) is the rightmost bit. Its place value is always 1, so flipping it changes the value by just 1.
In the pattern 10101 the MSB is the leftmost 1, worth 16, and the LSB is the final 1, worth 1. Reading the place values from left to right gives 16, 8, 4, 2 and 1, so the value is 1×16 + 0×8 + 1×4 + 0×2 + 1×1 = 21. Only that leftmost bit is worth 16, which is exactly why it earns the name “most significant”.
Hexadecimal is a base-16 number system, so it is built from 16 different symbols. Ten of them are the digits 0 to 9, standing for the values zero to nine. The other six are the letters A to F, standing for the values ten to fifteen. Once you reach F you have used every symbol, so you carry into the next column, just as 9 rolls over to 10 in denary.
The reason hexadecimal suits computing so well is that each hexadecimal digit represents exactly 4 bits, which is one nibble. There are 16 possible patterns of 4 bits and 16 hexadecimal symbols, so the two line up one-to-one and conversion is quick in both directions.
For example, take 1011010111. Grouping from the right gives 10 1101 0111, and padding the short group gives 0010 1101 0111. Now 0010 is 2, 1101 is D and 0111 is 7, so the pattern is 2D7. Going back, 2D7 becomes 0010 1101 0111, and dropping the leading zeros returns 1011010111.
Hexadecimal is a compact way of writing binary. One byte is eight bits, so it needs exactly two hexadecimal digits, and a long run of 0s and 1s can be copied, compared and checked as a much shorter string that is easier to read, easier to remember and less likely to be mistyped. Programmers meet it wherever binary values are shown or written, such as memory addresses, colour settings and error codes.
Hexadecimal does not reduce storage space: the computer stores exactly the same binary value, and only the written version gets shorter. It is a human-friendly notation, so any hexadecimal you type is converted to binary before the processor uses it.