• 沒有找到結果。

Bits. Dafa Types, and Operations

在文檔中 introduction to computing systems (頁 44-48)

2.1 Bits and Data T p s

2.1.1 The Bit as the Unit of Information

We noted in Chapter 1 that the computer was organized as a system with several levels of transformation. A problem stated in a natural language such as English is actually solved by the electrons moving around inside the electronics of the computer.

Inside the computer, millions of very tiny, very fast devices control the move-ment of those electrons. These devices react to the presence or absence of voltages in electronic circuits. They could react to the actual voltages, rather than simply to the presence or absence of voltages. However, this would make the control and detection circuits more complex than they need to be. It is much easier simply to detect whether or not a voltage exists between a pair of points in a circuit than it is to measure exactly what that voltage is.

To understand this, consider any wall outlet in your home. You could measure the exact voltage it is carrying, whether 120 volts or 115 volts, or 118.6 volts, for example. However, the detection circuitry to determine only whether there is a voltage (any of the above three will do) or whether there is no voltage is much simpler. Your finger casually inserted into the wall socket, for example, will suffice.

We symbolically represent the presence of a voltage as "1" and the absence of a voltage as "0." We refer to each 0 and each 1 as a "bit," which is a shortened form of binary digit. Recall the digits you have been using since you were a

child—0, 1, 2, 3 , . . . , 9. There are 10 of them, and they are referred to as decimal digits. In the case of binary digits, there are two of them, 0 and 1.

To be perfectly precise, it is not really the case that the computer differentiates the absolute absence of a voltage (that is, 0) from the absolute presence of a voltage (that is, 1). Actually, the electronic circuits in the computer differentiate voltages close to 0 from voltages far from 0. So, for example, if the computer expects a voltage of 2.9 volts or a voltage of 0 volts (2.9 volts signifying 1 and 0 volts signifying 0), then a voltage of 2.6 volts will be taken as a 1 and 0.2 volts will be taken as a 0.

To get useful work done by the computer, it is necessary to be able to identify uniquely a large number of distinct values. The voltage on one wire can represent uniquely one of only two things. One thing can be represented by 0, the other thing can be represented by 1. Thus, to identify uniquely many things, it is necessary to combine multiple bits. For example, if we use eight bits (corresponding to the voltage present on eight wires), we can represent one particular value as 01001110, and another value as 11100111. In fact, if we are limited to eight bits, we can differentiate at most only 256 (that is, 28) different values. In general, with k bits, we can distinguish at most 2k distinct items. Each pattern of these k bits is a code; that is, it corresponds to a particular value.

2.1.2 Data Types

There are many ways to represent the same value. For example, the number five can be written as a 5. This is the standard decimal notation that you are used to. The value five can also be represented by someone holding up one hand, with all fingers and thumb extended. The person is saying, "The number I wish to communicate can be determined by counting the number of fingers I am showing." A written version of that scheme would be the value 11111. This notation has a name also—unary. The Romans had yet another notation for five—the character V. We will see momentarily that a fourth notation for five is the binary representation 00000101.

It is not enough simply to represent values; we must be able to operate on those values. We say a particular representation is a data type if there are operations in the computer that can operate on information that is encoded in that representation.

Each ISA has its own set of data types and its own set of instructions that can operate on those data types. In this book, we will mainly use two data types:

2's complement integers for representing positive and negative integers that we wish to perform arithmetic on, and ASCII codes for representing characters on the keyboard that we wish to input to a computer or display on the computer's monitor. Both data types will be explained shortly.

There are other representations of information that could be used, and indeed that are present in most computers. Recall the "scientific notation" from high school chemistry where you were admonished to represent the decimal num-ber 621 as 6.21102. There are computers that represent numbers in that form, and they provide operations that can operate on numbers so represented. That data type is usually called floating point. We will show you its representation in Section 2.6.

2.2 Integer Data Types

2.2 Integer Data Types

2.2.1 Unsigned Integers

The first representation of information, or data type, that we shall look at is the unsigned integer. Unsigned integers have many uses in a computer. If we wish to perform a task some specific number of times, unsigned integers enable us to keep track of this number easily by simply counting how many times we have performed the task "so far." Unsigned integers also provide a means for identifying different memory locations in the computer, in the same way that house numbers differentiate 129 Main Street from 131 Main Street.

We can represent unsigned integers as strings of binary digits. To do this, we use a positional notation much like the decimal system that you have been using since you were three years old.

You are familiar with the decimal number 329, which also uses positional notation. The 3 is worth much more than the 9, even though the absolute value of 3 standing alone is only worth 1/3 the value of 9 standing alone. This is because, as you know, the 3 stands for 300 (3 * 102) due to its position in the decimal string 329, while the 9 stands for 9 • 10°.

The 2's complement representation works the same way, except that the digits used are the binary digits 0 and 1, and the base is 2, rather than 10. So, for example, if we have five bits available to represent our values, the number 6 is represented as 00110, corresponding to

0 • 24 + 0 • 23 + 1 • 22 + 1 • 21 + 0 • 2°

With k bits, we can represent in this positional notation exactly 2k integers, ranging from 0 to 2k — 1. In our five-bit example, we can represent the integers from 0 to 31.

2.2.2 Signed Integers

However, to do useful arithmetic, it is often (although not always) necessary to be able to deal with negative quantities as well as positive. We could take our 2k distinct patterns of k bits and separate them in half, half for positive numbers, and half for negative numbers. In this way, with five-bit codes, instead of representing integers from 0 to +31, we could choose to represent positive integers from + 1 to +15 and negative integers from —1 to —15. There are 30 such integers. Since 25 is 32, we still have two 5-bit codes unassigned. One of them, 00000, we would presumably assign to the value 0, giving us the full range of integer values from

— 15 to +15. That leaves one more five-bit code to assign, and there are different ways to do this, as we will see momentarily.

We are still left with the problem of determining what codes to assign to what values. That is, we have 32 codes, but which value should go with which code?

Positive integers are represented in the straightforward positional scheme.

Since there are k bits, and we wish to use exactly half of the 2k codes to represent the integers from 0 to 2k~l — 1, all positive integers will have a leading 0 in their representation. In our example (with k = 5), the largest positive integer +15 is represented as 01111.

Representation Value Represented

Signed Magnitude l's Complement 2's Complement

00000 0 0 0

00001 1 1 1

00010 2 2 2

00011 3 3 3

00100 4 4 4

00101 5 5 5

00110 6 6 6

00111 7 7 7

01000 8 8 8

01001 9 9 9

01010 10 10 10

01011 11 11 11

01100 12 12 12

01101 13 13 13

01110 14 14 14

01111 15 15 15

10000 - 0 - 1 5 - 1 6

10001 - 1 - 1 4 - 1 5

10010 - 2 - 1 3 - 1 4

10011 - 3 - 1 2 - 1 3

10100 - 4 - 1 1 - 1 2

10101 - 5 - 1 0 - 1 1

10110 - 6 - 9 - 1 0

10111 - 7 - 8 - 9

11000 - 8 - 7 - 8

11001 - 9 - 6 - 7

11010 - 1 0 - 5 - 6

11011 - 1 1 - 4 - 5

11100 - 1 2 - 3 - 4

11101 - 1 3 - 2 - 3

11110 - 1 4 - 1 - 2

11111 - 1 5 - 0 - 1

Figure 2.1 Three representations of signed integers

Note that in all three data types shown in Figure 2.1, the representation for 0 and all the positive integers start with a leading 0. What about the representations for the negative numbers (in our five-bit example, —1 to - 1 5 ) ? The first thought that usually comes to mind is: If a leading 0 signifies a positive integer, how about letting a leading 1 signify a negative integer? The result is the signed-magnitude data type shown in Figure 2.1. A second idea (which was actually used on some early computers such as the Control Data Corporation 6600) was the following:

Let a negative number be represented by taking the representation of the positive number having the same magnitude, and "flipping" all the bits. So, for example,

在文檔中 introduction to computing systems (頁 44-48)