How bits become letters, colours and codes
The same byte as a number, a letter, two decimal digits or a flag: a code decides.
A pattern means nothing on its own
So far every pattern of bits has been a number. That was a choice, not a fact about bits. The byte 01000010 is the number 66 only if everyone has agreed to read it as a number. Under a different agreement the same eight bits are the letter B, or the two decimal digits 4 and 2, or eight separate yes-or-no answers. The bits do not change. Only the reading does.
Letters: ASCII
To store text, a computer needs a code that pairs every character with a number. The one almost every system agrees on is ASCII, a table drawn up in the 1960s that gives 128 characters a number from 0 to 127. Capital A is 65, B is 66, and so on up to Z at 90. The small letters follow, with a at 97 and z at 122. The characters 0 to 9 are in the table too, with the character 0 at 48 and 9 at 57. That last part trips people up: the character 7 that you see on a screen is stored as the number 55, not as the number 7.
The codes from 32 to 126 are the ones that print: 32 is a space, and the rest are letters, digits and punctuation. Codes below 32 are instructions to the printer or screen, such as "new line", and 127 is "delete". None of those has a shape to draw, which is why the widget above says "not printable" for them. ASCII needs only seven bits for its 128 codes, but it is always stored in a byte, and the eighth bit was later used to extend the table.
Common mistake: the byte contains a letter
It does not. The byte contains 01000010, and a program that has been told to read it as ASCII draws a B. Hand the same byte to a program expecting a number and it will say 66. If two programs disagree about the code, the text comes out as rubbish, which is exactly what happens when a file is opened with the wrong character encoding.
Colour: three bytes
A colour on a screen is made by mixing red, green and blue light, and each of the three is stored as one byte: 0 means none of that light, 255 means as much as the pixel can give. So a colour is 3 bytes, which is 24 bits, and there are 224 = 16,777,216 colours to choose from. This is the code behind the hex colours in web pages: FF8000 is red 255, green 128, blue 0, which is orange. Each byte is two hex digits, so the three channels sit side by side and can be read off one at a time.
Decimal digits: BCD
Sometimes a circuit wants to show a decimal number to a person, on a clock or a meter. Working out the decimal digits of a binary number takes dividing by ten, and dividing is hard to build. Binary coded decimal, or BCD, sidesteps the problem: each decimal digit gets its own nibble, holding that digit as a four bit binary number. The number 42 becomes 0100 0010, a 4 and then a 2, rather than 101010.
A nibble has sixteen patterns and there are only ten digits, so six patterns are never used in BCD. They are the nibbles worth 10 to 15:
| Nibble | Unsigned value | In BCD |
|---|---|---|
| 1010 | 10 | not a digit |
| 1011 | 11 | not a digit |
| 1100 | 12 | not a digit |
| 1101 | 13 | not a digit |
| 1110 | 14 | not a digit |
| 1111 | 15 | not a digit |
That waste is the price of a simpler circuit: each nibble drives its own digit of the display directly. In stage 4, when you design the circuit that turns a BCD digit into the seven bars of a display, those six patterns will come back as inputs that can never happen, and you will be allowed to give them whatever output makes the circuit smallest.
Worked example. Read the byte 01000010 three ways.
As an unsigned number, the 1s sit under the weights 64 and 2, so it is 66. As ASCII, 65 is A, so 66 is B. As BCD, split it into nibbles: 0100 is 4 and 0010 is 2, so it is the decimal number 42. Three answers, one byte, and every one of them is right under its own code.
One bit on its own: a flag
The smallest code of all uses a single bit. A flag is one bit whose meaning is yes or no for one particular question: is the light on, is the door open, did the last sum overflow. Circuits keep such bits in a register, which is a row of bits a circuit holds on to until it is told to change them; stage 7 builds one. A byte in a register can hold eight unrelated flags at once, and a circuit that only cares about the light reads only its bit and ignores the other seven. Under this code the byte is not a number at all.
Why?: what about negative numbers?
They are a code too, and the one that matters is called two's complement. It reads the top bit of a pattern as a negative weight, so 11111111 is −1 rather than 255. It is left until stage 5 because the reason it is chosen over every other way of writing negatives only makes sense once you have built an adder and seen it wrap. For now, every number in this course is unsigned: zero or above.
What to remember
- Bits have no meaning until a code, an agreed table, gives them one. The same pattern means different things under different codes.
- ASCII numbers characters: A is 65, a is 97, and the character 0 is 48. Codes 32 to 126 are the printable ones.
- A screen colour is three bytes, one each for red, green and blue: 24 bits, so about 16.8 million colours.
- BCD gives each decimal digit its own nibble, and never uses the six nibbles worth 10 to 15.
- A flag is one bit meaning yes or no. Negative numbers are a code too, and wait until stage 5.
Check yourself
Get 5 right in a row and the lesson is done. A wrong answer costs the run, not the lesson.
A screen colour is stored as 3 channels, red, green and blue, of 8 bits each. How many bytes is one colour?
Already know this? and come back to the quiz any time.