Converting Between Binary and Decimal Numbers


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.

note

The remainder of this appendix only uses examples with binary numbers that require eight or fewer digits.


Converting from Binary to Decimal

Converting 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. Example of Binary-to-Decimal Conversion: 10101101

Value Associated with That Digit or Column

128

64

32

16

8

4

2

1

The number itself

1

0

1

0

1

1

0

1


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.

Table B-4. Converting 10101101 to Decimal: Multiplying Each Column and Then Adding Them Together

Value associated with that digit or column

128

64

32

16

8

4

2

1

The number itself

1

0

1

0

1

1

0

1

Product of two numbers in the same column

128

0

32

0

8

4

0

1

Sum of all numbers in third row

173


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:

Step 1.

Write down the powers of 2, in decimal, in the top row of a table, similar to Table B-4.

Step 2.

On the second line, write down the binary number that is to be converted, lining up each binary digit under the powers of 2.

Step 3.

Multiply each pair of numbers (the numbers in the same column) together and then total those products.

Table B-5. Decimal Powers of 2

Power of 2

Decimal Value

20

1

21

2

22

4

23

8

24

16

25

32

26

64

27

128

28

256


Converting from Decimal to Binary

Converting 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:

Step 1.

Find the decimal power of 2 that is closest to, but not larger than, the decimal number being converted.

Step 2.

Write down a binary 1 as the left-most binary digit of the new binary number.

Step 3.

Subtract that power of 2 from the original decimal number. (The resulting number will be called the remainder in this algorithm.)

Step 4.

Repeat the following steps until the steps have been completed for the case in which the power of 2 is 1 (20). As a result of passing through these steps repeatedly, another binary digiteither 0 or 1will be added to the right side of the binary number for each pass through this step:

a. If the next lower power of 2 (compared to the previous step) is larger than the remainder, write down a binary 0 as the next binary digit.

b. Otherwise, write down a binary 1 as the next digit. Also, subtract the current power of 2 from the remainder, with the resulting remainder being used as the remainder in the next pass through Step 4.

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:

1.

The closest power of 2 not bigger than 235 is 128.

2.

The first binary digit will be 1.

3.

The remainder is calculated as 235 128 = 107. The first pass through Step 4 of the algorithm will use this remainder value.

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.

Table B-6. First Three Steps of Converting Decimal 235 to Binary

Decimal Number to Be Converted

235

2x closest to number, not bigger than the number

128

Binary number

1

Remainder

107


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.

Table B-7. First Pass Through Step 4 of the Conversion Algorithm, with 26 (64) as the Power of 2

Decimal Number to Be Converted

235

 

Remainder (from previous step/column)

107

Next lower 2x value as compared to previous step

64

Binary number

1

1

Remainder

107

43


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.

Table B-8. Second Pass Through Step 4, for 25 (32)

Decimal Number to Be Converted

235

  

Remainder (from previous step/column)

107

43

Next lower 2x value as compared to previous step

64

32

Binary number

1

1

1

Remainder

107

43

11


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.

Table B-9. Third Pass Through Step 4, for 24 (16)

Decimal Number to Be Converted

235

   

Remainder (from previous step/column)

107

43

11

Next lower 2x value as compared to previous step

64

32

16

Binary number

1

1

1

0

Remainder

107

43

11

11


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.

Table B-10. Fourth, Fifth, and Sixth Passes Through Step 4

Decimal Number to Be Converted

235

      

Remainder (from previous step/column)

107

43

11

11

3

3

Next lower 2x value as compared to previous step

64

32

16

8

4

2

Binary number

1

1

1

0

1

0

1

Remainder

107

43

11

11

3

3

1


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.

Table B-11. Last Pass Through Step 4, for 20 (1)

Decimal Number to Be Converted

235

       

Remainder (from previous step/column)

 

107

43

11

11

3

3

1

Next lower 2x value as compared to previous step

 

64

32

16

8

4

2

1

Binary number

1

1

1

0

1

0

1

1

Remainder

107

43

11

11

3

3

1

0


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.

Table B-12. First Three Steps of Converting Decimal 100 to Binary

Decimal Number to Be Converted

100

2x closest to number, not bigger than

64

Binary number

1

Remainder

36


Table B-13. Passes Through Step 4 for 2x Values of 32, 16, 8, 4, and 2 of Decimal-to-Binary Conversion of Decimal 100

Decimal Number to Be Converted

100

     

Remainder (from previous step/column)

36

4

4

4

0

Next lower 2x value as compared to previous step

32

16

8

4

2

Binary number

1

1

0

0

1

0

Remainder

36

4

4

4

0

0


Table B-14. Final Pass Through Step 4 of Decimal-to-Binary Conversion of Decimal 100

Decimal Number to Be Converted

100

      

Remainder (from previous step/column)

36

4

4

4

0

0

Next lower 2x value as compared to previous step

32

16

8

4

2

1

Binary number

1

1

0

0

1

0

0

Remainder

36

4

4

4

0

0

0


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.




Computer Networking first-step
Computer Networking First-Step
ISBN: 1587201011
EAN: 2147483647
Year: 2004
Pages: 173
Authors: Wendell Odom

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net