Chapter 5
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.
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.
We then interpret bits as numbers by thinking of “off” as 0 and “on” as 1. For example,
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:
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,
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".
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 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.
3.143.1.57