# Connexions

You are here: Home » Content » Representing symbols by bits

### Recently Viewed

This feature requires Javascript to be enabled.

# Representing symbols by bits

Module by: Anders Gjendemsjø. E-mail the author

## Introduction

Often we want to represent data, e.g. characters, images, in a binary form. By binary form we mean representing by the symbols "0", and "1". Using binary representation allows us to conveniently store, retrieve, and manipulate them with a computer. To work with data in binary form we must have a fixed way of encoding (representing) a fixed data stream. The set of all binary sequences in a representation of some data is called a code. (Note that this has nothing to do with cryptology). Usually we refer to the data that we want to represent by bits as a source.

### Example 1: Representing English Characters

Let us consider a very practical example of the above ideas. Let our source be a stream of English characters. Now we want to represent this stream of characters as bits, say to store it on a computer or send it over the Internet. First we need to know the number of such characters, which is (traditonally) conveniently set to 128. The number 128 is obtained by summing upper case charachters (26), lower case (26), digits (10), brackets and punctuation (20), odd characters (14) (the "&" is an odd character), and control characters (32).

Obviously we need to have a unique representation of each of the 128 characters, this can e.g. be obtained by exhausting the 128 bit combinations which concatenating 7 bits give. Thus we have devised an 7-bit code. A well known 7-bit code is ASCII, short for "American Standard Code for Information Interchange". Adding a parity bit for error control to the ASCII code forms an 8-bit code. As an example, the representation of an "A" in ASCII is 1000001.

Now, one can ask whether the 7-bit ASCII code is an optimal representation in terms of using, on average, the minimum number of bits representing the English characters? We will return to this question later (in example 3).

## Minimal representation

When representing a source we want to use as few bits as possible, as this will imply that less disk space is required for storage or that transmission over the Internet is quicker. However, we do not want to use so few bits that the receiver cannot determine what was sent or stored.

So, for a given source what is the minimal representation? Here we consider the minimal representation as the representation that uses the minimum number of bits (on average) to encode the source without errors. According to Shannon's source coding theorem, a source that produces statistically independent outcomes, the minimum average number of bits per symbol is the entropy of the source! (A classical example of a source that produces statistically independent outcomes is throwing a die.)

Average indicates that the number of bits used for a specific symbol may be different from the number of bits representing another. E.g., as opposed to ASCII coding, we might represent an "A" with 7 bits, but an "E" with 3 bits. But it also implies that when you receive a series of symbols, the number you receive per time unit, say per second, will not be exactly the same, but averaged over a long term period, the rate is proportional to time with the rate per symbol as the proportionality constant.

Let us assume that we represent a symbol xnxn, with probability pnpn, by lnln bits. Then, the average number of bits spent per symbol will be

L-= n =1Npnln L n 1 N pn ln
(1)
We see that this equation is equal to the entropy if the code words are selected to have the lengths ln=logpn ln pn . Thus, if the source produces stochastically independent outcomes with probabilities pnpn, such that logpn pn is an integer, then we can easily find an optimal code as we show in the next example.

### Example 2: Finding a minimal representation

A four-symbol alphabet produces stochastically independent outcomes with the following probabilities. Prx1=12 x1 1 2 Prx2=14 x2 1 4 Prx3=18 x3 1 8 Prx4=18 x4 1 8 and an entropy of 1.75 bits/symbol. Let's see if we can find a codebook for this four-letter alphabet that satisfies the Source Coding Theorem. The simplest code to try is known as the simple binary code: convert the symbol's index into a binary number and use the same number of bits for each symbol by including leading zeros where necessary.

x 1 00 x 2 01 x 3 10 x 4 11 x 1 00 x 2 01 x 3 10 x 4 11
(2)
As all symbols are represented by 2 bits, obviously the average number of bits per symbol is 2. Because the entropy equals 1.751.75 bits, the simple binary code is not a minimal representation according to the source coding theorem. If we chose a codebook with differing number of bits for the symbols, a smaller average number of bits can indeed be obtained. The idea is to use shorter bit sequences for the symbols that occur more often, i.e., symbols that have a higher probability. One codebook like this is
x 1 0 x 2 10 x 3 110 x 4 111 x 1 0 x 2 10 x 3 110 x 4 111
(3)
Now L-=1×12+2×14+3×18+3×18=1.75 L 1 1 2 2 1 4 3 1 8 3 1 8 1.75 . We can reach the entropy limit! This should come as no surprise, as promised above, when logpnpn is an integer for all nn, the optimal code is easily found.

The simple binary code is, in this case, less efficient than the unequal-length code. Using the efficient code, we can transmit the symbolic-valued signal having this alphabet 12.5% faster. Furthermore, we know that no more efficient codebook can be found because of Shannon's source coding theorem.

### Example 3: Optimality of the ASCII code

Let us return to the ASCII codes presented in Example 1. Is the 7-bit ASCII code optimal, i.e., is it a minimal representation? The 7-bit ASCII code assign an equal length (7-bit) to all characters it represents. Thus, it would be optimal if all of the 128 characters were equiprobable, that is each character should have a probability of 1128 1 128 . To find out whether the characters really are equiprobable an analysis of all English texts would be needed. Such an analysis is difficult to do. However, the letter "E" is more probable than the letter "Z", so the equiprobable assumption does not hold, and the ASCII code is not optimal.

(A technical note: We should take into account that in English text subsequent outcomes are not stochastically independent. To see this, assume the first letter to be "b", then it is more probable that the next letter is "e", than "z". In the case where the outcomes are not stochastically independent, the formulation we have given of Shannon's source coding theorem is no longer valid, to fix this, we should replace the entropy with the entropy rate, but we will not pursue this here).

## Generating efficient codes

From Shannon's source coding theorem we know what the minimum average rate needed to represent a source is. But other than in the case when the logarithm of the probabilities gives an integer, we do not get any indications on how to obtain that rate. It is a large area of research to get close to the Shannon entropy bound. One clever way to do encoding is the Huffman coding scheme.

## Content actions

PDF | EPUB (?)

### What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks