Memory Addressing, Binary, and Hexadecimal Review

Download Memory Addressing, Binary, and Hexadecimal Review

Preview text

Memory Addressing, Binary, and Hexadecimal Review
You do not have to understand the concepts in this appendix to become well-versed in C++. You can master C++, however, only if you spend some time learning about the behind-the-scenes roles played by binary numbers. The material presented here is not difficult, but many programmers do not take the time to study it; hence, there are a handful of C++ masters who learn this material and understand how C++ works “under the hood,” and there are those who will never master the language as they could.
You should take the time to learn about addressing, binary numbers, and hexadecimal numbers. These fundamental principles are presented here for you to learn, and although a working knowledge of C++ is possible without knowing them, they greatly enhance your C++ skills (and your skills in every other programming language).

Appendix A o Memory Addressing, Binary, and Hexadecimal Review

After reading this appendix, you will better understand why different C++ data types hold different ranges of numbers. You also will see the importance of being able to represent hexadecimal numbers in C++, and you will better understand C++ array and pointer addressing.

Computer Memory
Each memory location inside your computer holds a single character called a byte. A byte is any character, whether it is a letter of the alphabet, a numeric digit, or a special character such as a period, question mark, or even a space (a blank character). If your computer contains 640K of memory, it can hold a total of approximately 640,000 bytes of memory. This means that as soon as you fill your computer’s memory with 640K, there is no room for an additional character unless you overwrite something.
Before describing the physical layout of your computer’s memory, it is best to take a detour and explain exactly what 640K means.

Memory and Disk Measurements

K means approximately 1000 bytes and exactly 1024 bytes.

By appending the K (from the metric word kilo) to memory measurements, the manufacturers of computers do not have to attach as many zeros to the end of numbers for disk and memory storage. The K stands for approximately 1000 bytes. As you will see, almost everything inside your computer is based on a power of 2. Therefore, the K of computer memory measurements actually equals the power of 2 closest to 1000, which is 2 to the 10th power, or 1024. Because 1024 is very close to 1000, computer-users often think of K as meaning 1000, even though they know it only approximately equals 1000.
Think for a moment about what 640K exactly equals. Practically speaking, 640K is about 640,000 bytes. To be exact, however, 640K equals 640 times 1024, or 655,360. This explains why the PC DOS command CHKDSK returns 655,360 as your total memory (assuming that you have 640K of RAM) rather than 640,000.


C++ By

M means approximately 1,000,000 bytes and exactly 1,048,576 bytes.

Because extended memory and many disk drives can hold such a large amount of data, typically several million characters, there is an additional memory measurement shortcut called M, which stands for meg, or megabytes. The M is a shortcut for approximately one million bytes. Therefore, 20M is approximately 20,000,000 characters, or bytes, of storage. As with K, the M literally stands for 1,048,576 because that is the closest power of 2 (2 to the 20th power) to one million.
How many bytes of storage is 60 megabytes? It is approximately 60 million characters, or 62,914,560 characters to be exact.

Memory Addresses
Each memory location in your computer, just as with each house in your town, has a unique address. A memory address is simply a sequential number, starting at 0, that labels each memory location. Figure A.1 shows how your computer memory addresses are numbered if you have 640K of RAM.

Figure A.1. Memory addresses for a 640K computer.
By using unique addresses, your computer can track memory. When the computer stores a result of a calculation in memory, it finds an empty address, or one matching the data area where the result is to go, and stores the result at that address.

Appendix A o Memory Addressing, Binary, and Hexadecimal Review
Your C++ programs and data share computer memory with DOS. DOS must always reside in memory while you operate your computer. Otherwise, your programs would have no way to access disks, printers, the screen, or the keyboard. Figure A.2 shows computer memory being shared by DOS and a C++ program. The exact amount of memory taken by DOS and a C++ program is determined by the version of DOS you use, how many DOS extras (such as device drivers and buffers) your computer uses, and the size and needs of your C++ programs and data.
Figure A.2. DOS, your C++ program, and your program’s data share the same memory.
Bits and Bytes
You now know that a single address of memory might contain any character, called a byte. You know that your computer holds many bytes of information, but it does not store those characters in the same way that humans think of characters. For example, if you type a letter W on your keyboard while working in your C++ editor, you see the W on-screen, and you also know that the W is stored in a memory location at some unique address. Actually, your computer does not store the letter W; it stores electrical impulses that stand for the letter W.

C++ By

The binary digits 1 and 0 (called bits) represent on and off states of electricity.

Electricity, which runs through the components of your computer and makes it understand and execute your programs, can exist in only two states—on or off. As with a light bulb, electricity is either flowing (it is on) or it is not flowing (it is off). Even though you can dim some lights, the electricity is still either on or off.
Today’s modern digital computers employ this on-or-off concept. Your computer is nothing more than millions of on and off switches. You might have heard about integrated circuits, transistors, and even vacuum tubes that computers have contained over the years. These electrical components are nothing more than switches that rapidly turn electrical impulses on and off.
This two-state on and off mode of electricity is called a binary state of electricity. Computer people use a 1 to represent an on state (a switch in the computer that is on) and a 0 to represent an off state (a switch that is off). These numbers, 1 and 0, are called binary digits. The term binary digits is usually shortened to bits. A bit is either a 1 or a 0 representing an on or an off state of electricity. Different combinations of bits represent different characters.
Several years ago, someone listed every single character that might be represented on a computer, including all uppercase letters, all lowercase letters, the digits 0 through 9, the many other characters (such as %, *, {, and +), and some special control characters. When you add the total number of characters that a PC can represent, you get 256 of them. The 256 ASCII characters are listed in Appendix C’s ASCII (pronounced ask-ee) table.
The order of the ASCII table’s 256 characters is basically arbitrary, just as the telegraph’s Morse code table is arbitrary. With Morse code, a different set of long and short beeps represent different letters of the alphabet. In the ASCII table, a different combination of bits (1s and 0s strung together) represent each of the 256 ASCII characters. The ASCII table is a standard table used by almost every PC in the world. ASCII stands for American Standard Code for Information Interchange. (Some minicomputers and mainframes use a similar table called the EBCDIC table.)
It turns out that if you take every different combination of eight 0s strung together, to eight 1s strung together (that is, from 00000000, 00000001, 00000010, and so on until you get to 11111110, and finally, 11111111), you have a total of 256 of them. (256 is 2 to the 8th power.)


Appendix A o Memory Addressing, Binary, and Hexadecimal Review

Each memory location in your computer holds eight bits each. These bits can be any combination of eight 1s and 0s. This brings us to the following fundamental rule of computers.
NOTE: Because it takes a combination of eight 1s and 0s to represent a character, and because each byte of computer memory can hold exactly one character, eight bits equals one byte.

To bring this into better perspective, consider that the bit pattern needed for the uppercase letter A is 01000001. No other character in the ASCII table “looks” like this to the computer because each of the 256 characters is assigned a unique bit pattern.
Suppose that you press the A key on your keyboard. Your keyboard does not send a letter A to the computer; rather, it looks in its ASCII table for the on and off states of electricity that represent the letter A. As Figure A.3 shows, when you press the A key, the keyboard actually sends 01000001 (as on and off impulses) to the computer. Your computer simply stores this bit pattern for A in a memory location. Even though you can think of the memory location as holding an A, it really holds the byte 01000001.

Your Keyboard


Figure A.3. Your computer keeps track of characters by their bit patterns.


C++ By
If you were to print that A, your computer would not send an A to the printer; it would send the 01000001 bit pattern for an A to the printer. The printer receives that bit pattern, looks up the correct letter in the ASCII table, and prints an A.
From the time you press the A until the time you see it on the printer, it is not a letter A! It is the ASCII pattern of bits that the computer uses to represent an A. Because a computer is electrical, and because electricity is easily turned on and off, this is a nice way for the computer to manipulate and move characters, and it can do so very quickly. Actually, if it were up to the computer, you would enter everything by its bit pattern, and look at all results in their bit patterns. Of course, it would be much more difficult for us to learn to program and use a computer, so devices such as the keyboard, screen, and printer are created to work part of the time with letters as we know them. That is why the ASCII table is such an integral part of a computer.
There are times when your computer treats two bytes as a single value. Even though memory locations are typically eight bits wide, many CPUs access memory two bytes at a time. If this is the case, the two bytes are called a word of memory. On other computers (commonly mainframes), the word size might be four bytes (32 bits) or even eight bytes (64 bits).
Summarizing Bits and Bytes A bit is a 1 or a 0 representing an on or an off state of electricity. Eight bits represents a byte. A byte, or eight bits, represents one character. Each memory location of your computer is eight bits (a single byte) wide. Therefore, each memory location can hold one character of data. Appendix C is an ASCII table listing all possible characters. If the CPU accesses memory two bytes at a time, those two bytes are called a word of memory.

Appendix A o Memory Addressing, Binary, and Hexadecimal Review
The Order of Bits
To further understand memory, you should understand how programmers refer to individual bits. Figure A.4 shows a byte and a two-byte word. Notice that the bit on the far right is called bit 0. From bit 0, keep counting by ones as you move left. For a byte, the bits are numbered 0 to 7, from right to left. For a double-byte (a 16bit word), the bits are numbered from 0 to 15, from right to left.
Figure A.4. The order of bits in a byte and a two-byte word.
Bit 0 is called the least-significant bit, or sometimes the low-order bit. Bit 7 (or bit 15 for a two-byte word) is called the most-significant bit, or sometimes the high-order bit.
Binary Numbers
Because a computer works best with 1s and 0s, its internal numbering method is limited to a base-2 (binary) numbering system. People work in a base-10 numbering system in the “real” world. The base-10 numbering system is sometimes called the decimal numbering system. There are always as many different digits as the base in a numbering system. For example, in the base-10 system, there are ten digits, 0 through 9. As soon as you count to 9 and run out of digits, you have to combine some that you already used. The number 10 is a representation of ten values, but it combines the digits 1 and 0.

C++ By
The same is true of base-2. There are only two digits, 0 and 1. As soon as you run out of digits, after the second one, you have to reuse digits. The first seven binary numbers are 0, 1, 10, 11, 100, 101, and 110.
It is okay if you do not understand how these numbers were derived; you will see how in a moment. For the time being, you should realize that no more than two digits, 0 and 1, can be used to represent any base-2 number, just as no more than ten digits, 0 through 9, can be used to represent any base-10 number in the regular numbering system.
You should know that a base-10 number, such as 2981, does not really mean anything by itself. You must assume what base it is. You get very used to working with base-10 numbers because you use them every day. However, the number 2981 actually represents a quantity based on powers of 10. For example, Figure A.5 shows what the number 2981 actually represents. Notice that each digit in the number represents a certain number of a power of 10.

Figure A.5. The base-10 breakdown of the number 2981.

A binary number can contain only the digits 1 and 0.

This same concept applies when you work in a base-2 numbering system. Your computer does this because the power of 2 is just as common to your computer as the power of 10 is to you. The only difference is that the digits in a base-2 number represent powers of 2 and not powers of 10. Figure A.6 shows you what the binary numbers 10101 and 10011110 are in base-10. This is how you convert any binary number to its base-10 equivalent.


Appendix A o Memory Addressing, Binary, and Hexadecimal Review
Figure A.6. The base-2 breakdown of the numbers 10101 and 10011110.
A base-2 number contains only 1s and 0s. To convert any base2 number to base-10, add each power of 2 everywhere a 1 appears in the number. The base-2 number 101 represents the base-10 number 5. (There are two 1s in the number, one in the 2 to the 0 power, which equals 1, and one in the 2 to the second power, which equals 4.) Table A.1 shows the first 18 base-10 numbers and their matching base-2 numbers.

Preparing to load PDF file. please wait...

0 of 0
Memory Addressing, Binary, and Hexadecimal Review