Info

The position of each digit in a binary number has a value assigned to it. Starting with the rightmost digit and moving left, the position values are 2°, 21, 22, 23, and so forth, as shown in Figure 1-9. Figure 1-10 shows the same diagram with the position values calculated. Starting with the rightmost digit and moving left, the position values are 1, 2, 4, 8, and so forth.

Figure 1-9 The values of binary digits as powers of 2

10 Chapter 1 Introduction to Computers and Programming Figure 1-10 The values of binary digits

To determine the value of a binary number you simply add up the position values of all the Is. For example, in the binary number 10011101, the position values of the Is are 1, 4, 8, 16, and 128. This is shown in Figure 1-11. The sum of all of these position values is 157. So, the value of the binary number 10011101 is 157.

Figure 1-11 Determining the value of 10011101

Figure 1-12 shows how you can picture the number 157 stored in a byte of memory. Each 1 is represented by a bit in the on position, and each 0 is represented by a bit in the off position.

Figure 1-12 The bit pattern for 157

128 + 16 + 8 + 4+ 1 =157

When all of the bits in a byte are set to 0 (turned off), then the value of the byte is 0. When all of the bits in a byte are set to 1 (turned on), then the byte holds the largest value that can be stored in it. The largest value that can be stored in a byte is 1+2+4 + 8 + 16 + 32 + 64 + 128 = 255. This limit exists because there are only eight bits in a byte.

What if you need to store a number larger than 255? The answer is simple: use more than one byte. For example, suppose we put two bytes together. That gives us 16 bits. The position values of those 16 bits would be 2°, 2\ 21, 2% and so forth, up through 215. As shown in Figure 1-13, the maximum value that can be stored in two bytes is 65,535. If you need to store a number iarger than this, then more bytes are necessary.

Figure 1-13 Two bytes used for a large number

Figure 1-13 Two bytes used for a large number

32768 + 16384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 65535

TIP: In case you're feeling overwhelmed by all this, relax! You will not have to actually convert numbers to binary while programming. Knowing that this process is taking place inside the computer will help you as you learn, and in the long term this knowledge will make you a better programmer.

Storing Characters

Any piece of data that is stored in a computer's memory must be stored as a binary number. That includes characters, such as letters and punctuation marks. When a character is stored in memory, it is first converted to a numeric code. The numeric code is then stored in memory as a binary number.

Over the years, different coding schemes have been developed to represent characters in computer memory. Historically, the most important of these coding schemes is ASCII, which stands for the American Standard Code for Information Interchange. ASCII is a set 128 numeric codes that represent the English letters, various punctuation marks, and other characters. For example, the ASCII code for the uppercase letter A is 65. When you type an uppercase A on your computer keyboard, the number 65 is stored in memory (as a binary number, of course).This is shown in Figure 1-14. p

Figure 1-14 The letter A is stored in memory as the number 65