Chapter 5

Computer Memory: Integers

It is time to go back to the machine level and look a little more deeply at memory. At a high level, computer memory may be categorized according to concepts such as access speed and size. Definite patterns emerge:

Type

Access Speed

Proximity to CPU

Size

Volatile

Register

Fastest

Inside

10’s

Y

Caches

Very fast

Adjacent

100’s to MB

Y

RAM

Fast

Near

GB

Y

Hard disks

Slow

Far

TB

N

At a lower level, the question is, how is data actually stored in computer memory? In this chapter, we begin to develop an answer by looking at how integers are stored.

Bits and Bytes

Computer memory of all types may be thought of at different levels of interpretation. The bottom level is electronics and physics, which is taught in those courses. We will move up one level of interpretation and begin by thinking of memory as a sequence of electronic on/off switches. Each on/off switch is called a bit. A group of 8 bits is called a byte.

In abbreviations, a small “b” refers to bits, while capital “B” refers to bytes. So, for example, Mbps refers to megabits per second, while GB refers to gigabytes.

Binary Numbers

We then interpret bits as numbers by thinking of “off” as 0 and “on” as 1. For example,

offononoffonoffoffoff01101000

We interpret this number as a number in base two instead of base ten. Now, the numbers that you use every day are decimal or base ten. Think about how they work:

2174= 2 * 1000 + 1 * 100 + 7 * 10 + 4 * 11000’s100’s10’s1’s103102101100

Binary numbers are base two and work the same way except that instead of powers of ten, they use powers of two. Note that with base ten, we use the digits 0–9 (less than ten); with base two, we only use the digits 0 and 1 (less than two). For example,

1011= 1 * 8 + 0 * 4 + 1 * 2 + 1 = 118’s4’s2’s1’s23222120

Hexadecimal Numbers

Hexadecimal numbers are base sixteen and work the same way with powers of sixteen and digits 0–9, A=10, B=11, C=12, D=13, E=14, and F=15. Every four bits can be thought of as a single hexadecimal “digit,” since four bits can hold values between 0 and 15. Thus, the byte 01101100 can be represented as 6C hexadecimal, since 0110 equals 6 and 1100 equals 12, which is C.

Python has built-in conversion functions that you may find useful:

bin(n)

Binary value of integer n (as a string).

hex(n)

Hex value of integer n (as a string).

Binary strings begin with "0b" in Python, while hex strings begin with "0x".

Storing Integers

When a computer is described as “32-bit” or “64-bit,” that tells you the basic memory size used by its CPU. Generally, this also gives the size of memory that is used to store an integer. Unsigned integers are always greater than or equal to zero and are stored in binary, as described above, using the number of bits given by the architecture. For example, on a 32-bit machine, the unsigned integer 2012 is stored as 0×000007dc.

Signed integers, on the other hand, may be either positive or negative and so require a more complicated representation. Most computer systems use two’s complement, which is taught in computer architecture courses.

Memory Sizes

Memory sizes are generally given in terms of bytes, except that so many bytes are involved that usually a prefix is used to indicate the scale at which we are working. Common prefixes are borrowed from the metric system, such as kilo-, mega-, giga-, and tera-. Unfortunately, these prefixes do not quite mean what they do in the metric system, where they are based on powers of 10:

Prefix

Value

kilo-

103 = 1000 = 1 thousand

mega-

106 = 1,000,000 = 1 million

giga-

109 = 1,000,000,000 = 1 billion

tera-

1012 = 1,000,000,000,000 = 1 trillion

Because computer memory is based on bits, powers of 2 are often used for these sizes instead:

Prefix

Value

kilo-

210 = 1024

mega-

220 = 1,048,576

giga-

230 = 1,073,741,824

tera-

240 = 1,099,511,627,776

These are close to their metric equivalents but are not exactly the same. This is one reason computer memory is sold in quantities that sometimes look strange.

Notice that 210 = 1024 ≈ 1000. This is a useful fact to help you remember the size of powers of 2.

Exercises

  1. 5.1 Using either your own computer or one in a lab, determine the number of registers in its CPU, as well as the size of its caches, RAM, and hard drive (or other long-term storage).
  2. 5.2 Give the largest binary value that can be stored in one unsigned byte, along with its decimal and hexadecimal equivalents.
  3. 5.3 Give the largest binary value that can be stored in two unsigned bytes, along with its decimal and hexadecimal equivalents.
  4. 5.4 Determine the largest unsigned integer that can be stored in a 32-bit machine.
  5. 5.5 Determine the largest unsigned integer that can be stored in a 64-bit machine.
  6. 5.6 Give the values of these Python expressions:
    1. (a) hex(25)
    2. (b) bin(35)
    3. (c) int("0×1C", 16)
    4. (d) int("0b10101", 2)
    5. (e) int(bin(1000), 2)
    6. (f) int(hex(1000), 16)
  7. 5.7 Show how each of these is stored as an unsigned integer in a byte. Write both binary and hexadecimal forms.
    1. (a) 87
    2. (b) 195
    3. (c) 18
    4. (d) 119
    5. (e) 93
    6. (f) 234
  8. 5.8 Convert these unsigned binary integers to decimal and hexadecimal.
    1. (a) 0b10010011
    2. (b) 0b00101101
    3. (c) 0b01001011
    4. (d) 0b10011110
    5. (e) 0b01011100
    6. (f) 0b11000001
  9. 5.9 Convert these unsigned hexadecimal integers to binary and decimal.
    1. (a) 0×7D
    2. (b) 0×A1
    3. (c) 0×59
    4. (d) 0×BC
    5. (e) 0×96
    6. (f) 0×04
  10. 5.10 Write a short program binhex.py that prints a table of binary and hexadecimal values for the (decimal) integers 1 through 100. You do not need any functions other than main().
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.1.57