Lecture 4: Digital Technology and Binary Representations

On the first day of class we defined a computer as, among other things, digital. Remember that digital devices, unlike analog ones that change smoothly, only change by making discrete transitions. But why is it important for computers to be digital? The answer is one of engineering rather than theory. Our goal is to build devices that are robust and stable. Error, uncertainty, and ambiguity should be avoided at all costs. For example, if the device's behavior is dependent on the voltage of current through a wire, it is harder to tell it what to do in response to every precise voltage than it is to instruct it to respond when it is above or below certain thresholds. Digital devices are simpler because they have fewer states or inputs that need to be programmed.

Another result from engineering that has influenced the core nature of computers is that the design of devices that can distinguish between two alternatives is far easier than in the case of many alternatives. The device will be simpler and cheaper to manufacture as well. This difference can be seen, for example, in the relative complexity of Charles Babbage's difference engine, which operated in the decimal number system, and comparable modern "calculators" that use the binary number system. As a result, virtually every device within a computer is capable of making only binary distinctions.

While this might seem like a severe limitation, the power of symbolic representation allows us to codify a broad range of such distinctions in a simple, uniform fashion. Consider the types of binary distinctions we might want to make: a current might have a voltage that is either high or low; a magnetic field might be polarized either positively or negatively; a switch might be either open or closed; a device might be on or off. There are even some binary distinctions not motivated by hardware considerations: a statement might be either true or false; your answer might be either yes or no; a symbol might be either 0 or 1.

In fact the symbols 0 and 1 have been chosen to represent all of the binary distinctions that ever exist in a computer! They also serve as the building blocks for other codes and representations. For this reason we say that a computer uses binary representations. A single binary digit (0 or 1) is called a bit. (Eight of them are called a byte.)

One of the most obvious binary representations is the binary number system. This is where a sequence of ones and zeros is interpreted a code for some natural number. We will examine the following aspects of the binary number system: counting, base-conversion, and addition. All of arithmetic could, in principle, be translated to binary.

Before we can begin looking at the binary number system, we need to understand something called radix notation. Radix notation is a generalization of the idea we all learned in elementary school in which every column of a number represents a power of the base. You most likely leaned it in terms of there being a one's-place, a ten's-place, a hundred's-place, etc., where ten is the base of our decimal number system. In other words we learned that 4379 = 4 * 1000 + 3 * 100 + 7 * 10 + 9. Or that 4379 = 4 * 103 + 3 * 102 + 7 * 101 + 9 * 100. Notice that 10 is the base of the exponents in each term. This is because we are assuming the decimal number system (also known as base-10). The binary number system uses a similar radix notation, but with 2 as the base (it is also called base-2). For example, 11012 = 1 * 23 + 1 * 22 + 0 * 21 + 1 * 20 = 8 + 4 + 1 = 1310 (We will use subscripts to identify the base of a number). In binary there is a one's-place, a two's-place, a four's-place, an eight's-place, etc.

As you can see the only difference between the two number systems is the base. Actually, the decimal number system is as much a code or representation for numbers as binary is. When there is only one system we tend to forget that there is a difference between a number and its representation. Because both systems are based on the radix notation it is easy to understand one if you already understand the other.

Take counting for example. The decimal number system has ten symbols (0, 1, 2, 3, 4, 5, 6, 7, 8, 9). To count you start, first, by listing all the symbols in order, then adding a column and starting over in the first column. You continue by incrementing a column every time you run out of symbols in the column to its right. Counting in binary is exactly the same, only with fewer symbols.
DecimalBinary
But how do we know that counting this way gives us the same numbers in the same order? We can use the radix notation to help us convert binary numbers into their equivalent decimal form. The algorithm for this conversion is as follows:

  1. Label each column in the number with the exponent of two that it represents.
  2. For each '1' in the binary number, write down its corresponding power of 2.
  3. Add up all the powers of two that were written down in step 2.
Binary Radix Conversion Simplify Decimal
0 0 * 20 0 0
1 1 * 20 1 1
10 1 * 21 + 0 * 20 2 2
11 1 * 21 + 1 * 20 2 + 1 3
100 1 * 22 + 0 * 21 + 0 * 20 4 4
101 1 * 22 + 0 * 21 + 1 * 20 4 + 1 5
110 1 * 22 + 1 * 21 + 0 * 20 4 + 2 6
111 1 * 22 + 1 * 21 + 1 * 20 4 + 2 + 1 7
1000 1 * 23 + 0 * 22 + 0 * 21 + 0 * 20 8 8
1001 1 * 23 + 0 * 22 + 0 * 21 + 1 * 20 8 + 1 9
1010 1 * 23 + 0 * 22 + 1 * 21 + 0 * 20 8 + 2 10
1011 1 * 23 + 0 * 22 + 1 * 21 + 1 * 20 8 + 2 + 1 11

This algorithms lets us convert any binary number into decimal. The table above shows that it works for the first few numbers. Let's try it on 101001112. 101001112 = 27 + 25 + 22 + 21 + 20 = 128 + 32 + 4 + 2 + 1 = 16710. Now we don't know if this is right without an algorithm for converting decimal numbers to binary. One method would be to simply count in binary as many numbers as the decimal number is big. The table above, for example could be used to count as high as eleven. This is impractical in general.

Another technique involves repeatedly dividing the decimal number by 2 and keeping the remainders:

  1. Divide the number by two, writing down both its quotient and remainder.
  2. Then divide the quotient by two, writing down both its quotient and remainder.
  3. Repeat step 2 until the quotient is zero.
  4. Copy the remainders in the reverse order you wrote then down. This is the binary equivalent.
167 ÷ 2 = 83 R 1
83 ÷ 2 = 41 R 1
41 ÷ 2 = 20 R 1
20 ÷ 2 = 10 R 0
10 ÷ 2 = 5 R 0
5 ÷ 2 = 2 R 1
2 ÷ 2 = 1 R 0
1 ÷ 2 = 0 R 1

So in this example we see that 16710 = 101001112, which confirms our previous result.

Finally, let's look at how the elementary school algorithm for addition carries over to binary numbers. In decimal, 347 + 95 = 442. Using the above algorithm we can convert 34710 into 1010110112 and 9510 into 10111112. In decimal addition the only way we know how to add two single digit numbers is to memorize all the combinations. For example we have all memorized that 7 + 5 = 12. In binary there are eight such combinations we need to know.


Then we proceed just as in decimal addition. Starting at the right we add up the digits in a column. If the sum is a single digit, just write it down. Otherwise, write down the least significant digit and carry the other digit to the next column.


The result of the addition is 1101110102 = 28 + 27 + 25 + 24 + 23 + 21 = 44210

Numbers and arithmetic are only one of the many possible applications of binary representations. Unfortunately, it is one of the few circumstances in which a pre-existing representation (decimal) will provide such enormous clues in developing a binary encoding. For example, there is little rhyme or reason to the way in which characters are encoded into a binary form. Various standards have been developed simply to provide compatibility.

The most common character code standard has been the ASCII (American Standard Code for Information Interchange) code, which assigns a unique 1-byte pattern of bits to each character. Since each bit in a byte can be either a one or a zero, there are 28, or 256, unique patterns available. Originally, only seven of the bits were used, allowing for 128 characters in the ASCII set. Eventually, the eighth bit was used, resulting in Extended ASCII. There is also a relatively new standard called Unicode, that uses two bytes to represent a character, allowing for as many as 65,536 unique patterns! For another explanation of binary representations click here. Later in the quarter we will even look at how things like color graphics and sounds are encoded in a binary format.

Before we leave this topic to take a look under the hood of a PC, let's make sure that we have some terminology straight. We have defined bit and byte, but there are several related terms that are commonly used: kilobyte, megabyte, gigabyte, etc. For a very good and complete explanation of these terms (and some more exotic ones also) click here.