You read earlier in this appendix that binary was simply a different way to write numbers as compared to decimal. In some cases, it's easier to work with one format or the other. However, you might only see the decimal version of the number, and you need to work with the binary version. Or the opposite could be true. That's why it's useful to be able to convert between the two formats.
Converting from Binary to DecimalConverting from binary to decimal is actually pretty straightforward, at least compared to converting from decimal to binary. In fact, you've already seen the math in the text after Table B-2. To convert a binary number to decimal, you have to think about the binary number in a table, such as Table B-3, and apply what the table's numbers mean.
Table B-3 looks just like Table B-2, except the binary number is slightly different. To convert to decimal, you simply multiply each pair of numbers that are in the same column in the table and then add them together. Table B-4 repeats the same information, but now with the products shown.
The process is indeed simple, as long as you remember all the powers of 2! When you're working with IP addressing, you'll need to memorize all the powers of 2 up through 28, or 256. (The lowest powers of 2 are listed in Table B-5 later in this appendix.) The basic algorithm to convert binary to decimal can be summarized as follows:
Converting from Decimal to BinaryConverting from decimal to binary doesn't take any difficult math, but the algorithm is a little longer. In this section, you'll read about a general algorithm for converting from decimal to binary, followed by a couple of examples. The general algorithm is as follows:
To appreciate how to use this algorithm, you need to either know the powers of 2 or have them listed somewhere handy. Table B-5 lists the powers of 2 that will be used in this appendix. The first example will be the conversion of decimal 235 into binary 11101011. To make the conversion happen, the following describes the first 3 steps of the algorithm:
Although describing these first three steps might be helpful, putting the same information into a table can help as well. Because the eventual binary number will be an eight-digit binary number in this example, the table will have places for eight digits. (When you use this algorithm, you might not know how many digits the new binary number will have, so leave plenty of space on the right side of your paper.) Table B-6 shows the details of the first three steps.
You do Step 4 repetitively for each successive lower power of 2, until you complete Step 4 for 20, or 1. Because the first 3 steps used 27 (128), the first pass through Step 4 uses 26 (64) as the power of 2, as shown in Table B-7.
This first pass through Step 4 adds another digit to the converted binary numberin this case, a binary 1. The next lower power of 2 is 64 in this case. The remainder from the previous step was 107, so from the generic algorithm, Step 4A is not used because 64 is not bigger than 107. Step 4B is used, so the next digit's value is a binary 1, and a new remainder (107 64 = 43) is calculated. Next, Step 4 is repeated with the next lower power of 2, 25 (32), as shown in Table B-8.
As in Table B-7, the pass through Step 4 shown in Table B-8 adds another binary 1 to the binary number, with the remainder being recalculated. In the next step, for 24 (16), the power of 2 is bigger than the remainder, yielding a binary 0 for the next digit. Table B-9 shows the values.
You're halfway through the example now, but hopefully, the general idea of how Step 4 works is becoming more obvious. Table B-10 summarizes the next three passes through Step 4.
With a power of 2 of 23, another binary 1 was added to the binary number. For 22 (4), 4 is larger than the remainder of 3, so a binary digit of 0 was added to the number. Finally, in the third of the 3 bold columns, for 21 (2), 2 is less than the remainder of 3, causing a binary digit of 1 to be added, with the remainder being recalculated as 1. The final pass through Step 4 is shown in Table B-11. The logic is not any different from the other passes through Step 4, other than the fact that you should know that this is the last time to use Step 4 because the next lower power of 2, 20 (1), is the last possible digit in a binary number.
With the completion of this step, the whole binary conversion process is complete. Binary number 11101011 is the binary equivalent of decimal 235. The next example shows how to convert decimal 100 to binary. Table B-12 shows Steps 1 through 3. Table B-13 shows the passes through Step 4 except for the final pass. Table B-14 shows the final pass through Step 4.
As you see from the final result in Table B-14, decimal 100 has a binary equivalent of the 7-digit number 1100100. Now that you have seen how to convert from decimal to binary, and vice versa, this appendix will close with an examination of how to convert decimal IP addresses to their binary version, and vice versa. |