A levels Computer Science · Data representation
Character sets
1Character codes
Computers store text as numbers. A character set is the list of characters a computer can use, with a unique binary code for each.
ASCII uses 7 bits, giving 2⁷ = 128 characters: English letters, digits, punctuation and control codes (such as new line).
| Character | Denary code | Binary |
|---|---|---|
| '0' | 48 | 011 0000 |
| 'A' | 65 | 100 0001 |
| 'a' | 97 | 110 0001 |
Codes run in order, so 'B' = 66, 'C' = 67, and lowercase letters are always 32 more than their capitals. This makes sorting and changing case easy.
Check yourself
If 'A' is 65, what is the code for 'E'?
Show the answer
69
Four letters later.
2Unicode
ASCII cannot represent other alphabets, accented letters, maths symbols or emoji. Unicode gives every character in every writing system a unique number, called a code point, with room for over a million.
| Encoding | Bits per character | Notes |
|---|---|---|
| UTF-8 | 8, 16, 24 or 32 (variable) | the first 128 codes match ASCII, so English text stays small |
| UTF-16 | 16 or 32 | common inside Windows and Java |
| UTF-32 | 32 (fixed) | simple, but uses the most space |
UTF-8 is the standard on the web: it is backward compatible with ASCII and efficient for mostly-English text.
Check yourself
Why does UTF-8 store English text as compactly as ASCII?
Show the answer
The first 128 code points use one byte and match ASCII
Backward compatible.
3Character codes versus numbers
The character '7' and the number 7 are stored differently:
- the character '7' has code 55, stored as 011 0111;
- the integer 7 is stored in pure binary as 0000 0111.
You cannot do arithmetic directly on character codes: "7" + "3" gives "73" (concatenation), but 7 + 3 gives 10. A program must convert text input to a number first, for example int("7").
To get the value of a digit character, subtract the code for '0': 55 − 48 = 7.
Check yourself
What is ord("9") − ord("0")?
Show the answer
9
Digit value.
4Parity bits
Data can be corrupted when it is sent or stored. A parity bit is an extra bit added so that the total number of 1s is even (even parity) or odd (odd parity).
Even parity. The parity bit is chosen so the whole byte has an even number of 1s.
Even parity for 'C'
- 'C' = 67 = 100 0011, which has three 1s.
- For even parity, add a 1 so the total is four: 1100 0011.
- If the receiver counts an odd number of 1s, it knows an error happened and asks for the data again.
Limitation: if two bits flip, the count is still even, so the error is not detected. Parity also cannot say which bit is wrong.
Check yourself
With odd parity, what parity bit is added to 101 0110?
Show the answer
1
Four 1s, make it five.
5Majority voting and check digits
Majority voting sends each bit three times (0 becomes 000). The receiver takes the value that appears most often, so 010 is read as 0. This can correct single errors, but triples the data sent.
A check digit is calculated from the other digits and added to the end, as in barcodes and ISBNs.
ISBN-13 check digit for 978-0-306-40615-?
- Multiply the digits alternately by 1 and 3: 9×1 + 7×3 + 8×1 + 0×3 + 3×1 + 0×3 + 6×1 + 4×3 + 0×1 + 6×3 + 1×1 + 5×3 = 93.
- Find the remainder when divided by 10: 3.
- Check digit = 10 − 3 = 7.
Check digits catch most typing mistakes, such as one wrong digit or two digits swapped.
Check yourself
Majority voting receives 110. Which bit is taken?
Show the answer
1
Two out of three.
6Checksums
A checksum is a value calculated from a whole block of data (for example by adding up the bytes) and sent with it. The receiver recalculates it; if the values differ, the block is resent.
| Method | Detects errors? | Corrects errors? | Extra data |
|---|---|---|---|
| parity bit | single-bit errors | no | 1 bit per byte |
| majority voting | yes | single errors | 3 times the data |
| check digit | most typing errors | no | 1 digit |
| checksum | most errors in a block | no | a few bytes per block |
More advanced checksums such as CRC (cyclic redundancy check) are used in networks and storage, and detect almost all errors.
Check yourself
Which method can correct an error, not just detect it?
Show the answer
Majority voting
Takes the most common value.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera