Wednesday, April 18, 2012

Everything is a number.

Computers do many amazing things. They are clearly very good with manipulating numbers. They can do calculations that would take us a lifetime to do with paper and pencil just in seconds. They can search ridiculously large amounts of information and get back an answer to a query almost instantaneously. They can let you play and edit songs and videos. They can play video games with you. They can steer trains, cars and airplanes. They can also beat the best human chess player in chess and the best human Jeopardy! player in Jeopardy!

How come they are so versatile? As a computer science professor if I had to give a one-short-sentence explanation for this amazing diversity of computers' capabilities, it would be: Everything is a number.

You may be thinking that I have some kind of fancy numbers in my mind when I say this. But I don't. I am not talking about irrational, or complex, or imaginary, or transcendental, or some other type of fancy numbers. I am not even thinking about real numbers. I am thinking about good old natural numbers which start with 0 and can be obtained by just adding one to the previous number in the sequence like so: 0, 1, 2, 3, . . .

I said "Everything is a number." and everything is a lot to cover, so let's start. The sentence "Everything is a number." is itself a number. The sentence consists of a sequence of alphabet symbols and blank spaces followed by a period at the end.  There is something called American Standard Code for Information Interchange (in short ASCII code) which assigns a number to each letter of the alphabet and each punctuation symbol. In the ASCII encoding, the lower case letter "i" corresponds to the number 105 and the lower case letter "s" corresponds to the number 115. So the word "is" which is concatenation of "i" and "s" corresponds to the number 105115 (which I obtained by concatenating 105 and 115). In the ASCII encoding, the code for " " (the space we leave between words) is the number 32 and the code for the period is 46.  Using the ASCII encoding, the number for the sentence "Everything is a number." happens to be 69118101114121116104105110103032105115032097032110117109098101114046. Seems like a pretty large number, but, then, I did not claim that everything was a small number.

Following this approach we can map any text to a single number, we just keep concatenating the numbers that correspond to the characters and the punctuation symbols. Keep in mind that we will never run out of numbers because there is an infinite number of numbers out there! There is a number for each text. There is a number for Cervantes' Don Quixote and there is a number for Shakespeare's Macbeth. There is a number for any text that anyone has ever written. I can even give you a finite range of numbers and claim that any novel with a reasonable length that anyone has ever written or will ever write is a number within that finite range. So, if you are an author who is having writer's block, there is nothing to worry about, you can just pick a number from that range and map it back to text based on the ASCII encoding, and there, you have a novel. If you use this strategy to write novels, your novels are very likely to be unreadable. But the point of this post is not to discuss which method to use to write novels. The point is, if you do ever write a novel, it will be a number within that finite range.

There is one issue I need to clarify. ASCII code is not the only way one can map characters to numbers. So, although I showed you that there is a way to map any text to a number using the ASCII code, that is not the only way. There are many ways one can do it. So, mapping between the texts and the numbers depends on the encoding you use. Once you decide on the encoding, you can map any text to a number and you can map any number back to the text it represents.

OK, so far we covered written text. What next? How about speech? Or, let's go even further and cover every sound. Yes, there is a number for every sound. Here is one way of mapping a sound to a number. First record the sound using a recording device, and then convert the recording to the MP3 format (the format used by music players like iPod). If you open the MP3 file and look at the contents of it, you will see a very long sequence of 0s and 1s. The MP3 file is a very very very long binary number. So what ever the sound you recorded, it is converted to a number when you store it in the MP3 format.

Conversion of the sounds to numbers are a little more complicated than the conversion of the text. I cannot tell you how it is done for the MP3 format in particular but I can give you a general idea. Any sound we hear corresponds to a wave that travels in air. It is kind of like a wave in the ocean, it has peaks (the highest points of a wave) and troughs (the lowest points of a wave). To convert a sound into a number, you need to measure the height of the sound wave frequently and store those height measurements as numbers one after the other.

The question is how frequently should we measure the height of the wave? If you do not measure frequently enough, you may only measure the wave at its peaks and then you may get the wrong impression that there is no wave at all and the ocean is still, or in our case we might think that it is silent. Luckily, there is a result from the area of signal processing that tells us how frequently we need to measure the height of a wave in order to capture it perfectly. It depends on how rapidly the wave changes from a peak to a trough. You just have to measure the height of the wave two times as fast as that. It turns out, humans cannot hear sound waves that change from a peak to a trough more than twenty thousand times a second and less than twenty times a second. So we do not need to bother with sound waves that are not in that range. This means that it is enough to measure the height of the sound wave forty thousand times a second if we only care about recording a sound for humans. That is a lot of measurements.

There is also the question of how to convert the height of the wave to a number. How precise should we be in measuring the height? It has been observed that a binary number that contains 16 binary digits would give us pretty good precision for storing the height of the wave. Let's recap. One second of sound can be represented with a number that is the concatenation of forty thousand sixteen digit binary numbers. That is one large number, but again, do not worry, we will never run out of numbers. Using this approach, we can convert any sound, no matter how long, to a (very large) number by writing the height measurements one after another. In principle, this is how your MP3 files represent a recording. They also do a lot of clever things to make the resulting numbers smaller but I will not be able to get into those here.

What about images and videos? How do we map them to numbers? First, a video is a concatenation of images like a text is a concatenation of characters. For example each second of a DVD video consists of 25 images. So if we can map an image to a number then we can map a video to a number.

How do we map an image to a number? Did you ever try to manually copy a picture by dividing it into little squares? When you make the squares small enough the image in each square becomes simple enough that you can copy it easily. If you keep making the squares smaller, at the end you will get a square which is just a single color. This is what computers do, they divide an image to small picture elements (called pixels) that can be represented as a single color. It turns out that many colors can be represented as a combination of red, green and blue. To convert an image to a number, you first divide it into small rectangles called pixels, and then for each pixel you measure how much red, how much green and how much blue you need to get the color of that pixel. The amount of red, green and blue needed can all be recorded as numbers and together they represent the number for that pixel. Then, the number for the whole image is the concatenation of all the numbers for all its pixels.

The things I discussed so far are all called data. Computers store data as numbers. It does not mater what type of data, computers convert all data to numbers. In fact, they convert them to numbers stored in binary format as sequences of 0s and 1s. But what about processing of data? The programs that process the data, how are they stored? Interestingly enough, computers also store programs as numbers! Actually this is not very difficult to see. Programs are typically written very much like regular text using characters of the alphabet and punctuation symbols. The languages they are written in are different than the natural languages like English, but, still, the programs are written like any other text. Since we know that any text can be mapped to a number, then clearly every program can also be mapped to a number and that is exactly what computers do, they store programs also as numbers like they store data.

So far I discussed how computers can represent information about the real world as numbers and represent the programs that do things with that data also as numbers. If you think about it, this is a very general concept and in addition to explaining how computers store and play music and video files it also explains how computers can be used for many other things, like physical simulations. For example, gathering as much information about the weather as we can by measuring anything related to it, and then using this data and the rules of physics for weather forecasting is another application of this concept. But does this cover everything? Well, now let's speculate a little. There is this idea that physicists have been working on for a long time: Finding the theory of everything. This would be a set of rules that explain everything that happens in our universe. If they do find these rules, we can implement a program that simulates these rules and map that program to a number. Then, if we can measure everything we can about the universe, and map those to a number, we would end up having a number for everything and a computer that can run the universe for us! Well, that is a lot of "if"s, but it is plausible!


  1. This idea that everything is a number is quite mind twisting. As I type this comment, I wonder whether the words I am writing are 1's or 0's. The amount of encoding that goes into computers and the software they run on is massive. However, by learning of the different types of encoding, such as ASCII, we can be exposed to and understand the technology we use in our every day life.

  2. This is a great seminar. With every class I get more answers to little questions about computers which have always bothered me. This was true this week especially with the descriptions of how images and audio are translated to numerical values. There are still gaps, however. Maybe each time I bring this up I'm asking too high-level a question for this particular seminar, but it remains the main block between me and understanding computers: physically, what is it inside of a computer which acts as the pages of the Turing Machine? How does an immobile chip inside of a computer represent the variability of the programs it runs?

  3. Considering that all things can be categorized by numbers begs the question as if numbers were replaced by other symbols or equally representative icons. For example, if we did calculations with letters, would all things be considered a letter? Imagine binary code through all As and Bs instead of 0s and 1s being standard; it's an interesting concept in itself! That being said, this seminar is very informative and a lot of information is packed into each 50 minute session. Mapping images and sounds through numbers is something I've never thought about.

  4. I was really interested in the MP3 recording and how the computer translates sound waves into 1's and 0's. I do small-time recording with a small set of mics and equipment I brought to UCSB and ever since the last seminar, I spent a good half of my recording time with my mind wandering about what the computer was doing to work with the mics and sound files. Proved to be a rather unproductive session recording-wise, but a very interesting one instead. I'm used to looking at sound waves on the computer screen so it makes perfect sense that they would have to be stored in binary, but I never realized how cool that is.