impera Open Impera

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).

CharacterDenary codeBinary
'0'48011 0000
'A'65100 0001
'a'97110 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'?

  • 101

  • 69

  • 70

  • 5

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.

EncodingBits per characterNotes
UTF-88, 16, 24 or 32 (variable)the first 128 codes match ASCII, so English text stays small
UTF-1616 or 32common inside Windows and Java
UTF-3232 (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?

  • It compresses the text using a dictionary of common words

  • The first 128 code points use one byte and match ASCII

  • It uses 4 bytes for every character, the same as ASCII

  • It only stores capital letters, which need fewer bits

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")?

  • 0

  • 57

  • 48

  • 9

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.

Worked example

Even parity for 'C'

  1. 'C' = 67 = 100 0011, which has three 1s.
  2. For even parity, add a 1 so the total is four: 1100 0011.
  3. 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?

  • It depends on the first bit

  • 1

  • 2

  • 0

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.

Worked example

ISBN-13 check digit for 978-0-306-40615-?

  1. 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.
  2. Find the remainder when divided by 10: 3.
  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?

  • 0

  • 1

  • Both

  • It asks again

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.

MethodDetects errors?Corrects errors?Extra data
parity bitsingle-bit errorsno1 bit per byte
majority votingyessingle errors3 times the data
check digitmost typing errorsno1 digit
checksummost errors in a blocknoa 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?

  • Majority voting

  • A simple checksum

  • A parity bit

  • A check digit

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