This appendix describes how to convert between the binary and decimal numbering systems, and includes the following sections:
Binary numbers are used by computers, including routers and switches, for their IP addresses. Thus, when working with and designing IP addresses, you need to understand how to work with binary numbers. People usually prefer to use decimal numbers, so converting between the binary and decimal numbering systems is a useful networking skill.
This appendix describes the binary and decimal numbering systems, and explains how to convert between the two.
The easiest way to convert between decimal and binary numbers is to use Table C-1.
Table C-1. Decimal-to-Binary Conversion Chart
Decimal | Binary | Decimal | Binary | Decimal | Binary | Decimal | Binary |
---|---|---|---|---|---|---|---|
0 | 00000000 | 10 | 00001010 | 20 | 00010100 | 30 | 00011110 |
1 | 00000001 | 11 | 00001011 | 21 | 00010101 | 31 | 00011111 |
2 | 00000010 | 12 | 00001100 | 22 | 00010110 | 32 | 00100000 |
3 | 00000011 | 13 | 00001101 | 23 | 00010111 | 33 | 00100001 |
4 | 00000100 | 14 | 00001110 | 24 | 00011000 | 34 | 00100010 |
5 | 00000101 | 15 | 00001111 | 25 | 00011001 | 35 | 00100011 |
6 | 00000110 | 16 | 00010000 | 26 | 00011010 | 36 | 00100100 |
7 | 00000111 | 17 | 00010001 | 27 | 00011011 | 37 | 00100101 |
8 | 00001000 | 18 | 00010010 | 28 | 00011100 | 38 | 00100110 |
9 | 00001001 | 19 | 00010011 | 29 | 00011101 | 39 | 00100111 |
40 | 00101000 | 71 | 01000111 | 102 | 01100110 | 133 | 10000101 |
41 | 00101001 | 72 | 01001000 | 103 | 01100111 | 134 | 10000110 |
42 | 00101010 | 73 | 01001001 | 104 | 01101000 | 135 | 10000111 |
43 | 00101011 | 74 | 01001010 | 105 | 01101001 | 136 | 10001000 |
44 | 00101100 | 75 | 01001011 | 106 | 01101010 | 137 | 10001001 |
45 | 00101101 | 76 | 01001100 | 107 | 01101011 | 138 | 10001010 |
46 | 00101110 | 77 | 01001101 | 108 | 01101100 | 139 | 10001011 |
47 | 00101111 | 78 | 01001110 | 109 | 01101101 | 140 | 10001100 |
48 | 00110000 | 79 | 01001111 | 110 | 01101110 | 141 | 10001101 |
49 | 00110001 | 80 | 01010000 | 111 | 01101111 | 142 | 10001110 |
50 | 00110010 | 81 | 01010001 | 112 | 01110000 | 143 | 10001111 |
51 | 00110011 | 82 | 01010010 | 113 | 01110001 | 144 | 10010000 |
52 | 00110100 | 83 | 01010011 | 114 | 01110010 | 145 | 10010001 |
53 | 00110101 | 84 | 01010100 | 115 | 01110011 | 146 | 10010010 |
54 | 00110110 | 85 | 01010101 | 116 | 01110100 | 147 | 10010011 |
55 | 00110111 | 86 | 01010110 | 117 | 01110101 | 148 | 10010100 |
56 | 00111000 | 87 | 01010111 | 118 | 01110110 | 149 | 10010101 |
57 | 00111001 | 88 | 01011000 | 119 | 01110111 | 150 | 10010110 |
58 | 00111010 | 89 | 01011001 | 120 | 01111000 | 151 | 10010111 |
59 | 00111011 | 90 | 01011010 | 121 | 01111001 | 152 | 10011000 |
60 | 00111100 | 91 | 01011011 | 122 | 01111010 | 153 | 10011001 |
61 | 00111101 | 92 | 01011100 | 123 | 01111011 | 154 | 10011010 |
62 | 00111110 | 93 | 01011101 | 124 | 01111100 | 155 | 10011011 |
63 | 00111111 | 94 | 01011110 | 125 | 01111101 | 156 | 10011100 |
64 | 01000000 | 95 | 01011111 | 126 | 01111110 | 157 | 10011101 |
65 | 01000001 | 96 | 01100000 | 127 | 01111111 | 158 | 10011110 |
66 | 01000010 | 97 | 01100001 | 128 | 10000000 | 159 | 10011111 |
67 | 01000011 | 98 | 01100010 | 129 | 10000001 | 160 | 10100000 |
68 | 01000100 | 99 | 01100011 | 130 | 10000010 | 161 | 10100001 |
69 | 01000101 | 100 | 01100100 | 131 | 10000011 | 162 | 10100010 |
70 | 01000110 | 101 | 01100101 | 132 | 10000100 | 163 | 10100011 |
10100100 | 187 | 10111011 | 210 | 11010010 | 233 | 11101001 | |
165 | 10100101 | 188 | 10111100 | 211 | 11010011 | 234 | 11101010 |
166 | 10100110 | 189 | 10111101 | 212 | 11010100 | 235 | 11101011 |
167 | 10100111 | 190 | 10111110 | 213 | 11010101 | 236 | 11101100 |
168 | 10101000 | 191 | 10111111 | 214 | 11010110 | 237 | 11101101 |
169 | 10101001 | 192 | 11000000 | 215 | 11010111 | 238 | 11101110 |
170 | 10101010 | 193 | 11000001 | 216 | 11011000 | 239 | 11101111 |
171 | 10101011 | 194 | 11000010 | 217 | 11011001 | 240 | 11110000 |
172 | 10101100 | 195 | 11000011 | 218 | 11011010 | 241 | 11110001 |
173 | 10101101 | 196 | 11000100 | 219 | 11011011 | 242 | 11110010 |
174 | 10101110 | 197 | 11000101 | 220 | 11011100 | 243 | 11110011 |
175 | 10101111 | 198 | 11000110 | 221 | 11011101 | 244 | 11110100 |
176 | 10110000 | 199 | 11000111 | 222 | 11011110 | 245 | 11110101 |
177 | 10110001 | 200 | 11001000 | 223 | 11011111 | 246 | 11110110 |
178 | 10110010 | 201 | 11001001 | 224 | 11100000 | 247 | 11110111 |
179 | 10110011 | 202 | 11001010 | 225 | 11100001 | 248 | 11111000 |
180 | 10110100 | 203 | 11001011 | 226 | 11100010 | 249 | 11111001 |
181 | 10110101 | 204 | 11001100 | 227 | 11100011 | 250 | 11111010 |
182 | 10110110 | 205 | 11001101 | 228 | 11100100 | 251 | 11111011 |
183 | 10110111 | 206 | 11001110 | 229 | 11100101 | 252 | 11111100 |
184 | 10111000 | 207 | 11001111 | 230 | 11100110 | 253 | 11111101 |
185 | 10111001 | 208 | 11010000 | 231 | 11100111 | 254 | 11111110 |
186 | 10111010 | 209 | 11010001 | 232 | 11101000 | 255 | 11111111 |
The rest of this appendix introduces and describes how to make this conversion from “first principles,” in other words, without the use of a conversion chart like Table C-1.
We first analyze the decimal numbers that we are familiar with. Decimal numbers are known as base 10 numbers. Within all numbering systems, the symbols used as digits range from 0 up to one less than the base; thus, within decimal numbers, the following ten unique symbols are used as digits:
0 1 2 3 4 5 6 7 8 9
When multiples of these symbols are combined to create a decimal number, each digit in the number is weighted based on its position within the number. The weights used are all powers of 10 (which is what base 10 means). The rightmost digit, which is the least significant, has a weight of 100 (1). The next digit has a weight of 101 (10). The next has a weight of 102 (100), and so on.
For example, consider the decimal number 5746. Figure C-1 illustrates how this number is interpreted.
Figure C-1 describes the following points:
The least significant digit, 6, has a weight of 100; thus, it represents 6 * 100 = 6 * 1 = 6.
The next digit, 4, has a weight of 101; thus, it represents 4 * 101 = 4 * 10 = 40.
The next digit, 7, has a weight of 102; thus it represents 7 * 102 = 7 * 100 = 700.
The most significant digit, 5, has a weight of 103; thus, it represents 5 * 103 = 5 * 1000 = 5000.
The decimal number is the sum of these representations: 5000 + 700 + 40 + 6 = 5746.
We read this number as “five thousand seven hundred and forty-six.”
The next section provides a similar analysis of binary numbers.
Binary numbers are base 2 numbers; within binary numbers, only the following two unique symbols are used as digits:
0 1
Just as for decimal numbers, when multiple binary symbols are combined to create a binary number, each digit in the number is weighted based on its position within the number. The weights used are all powers of 2 (which is what base 2 means). The rightmost digit, which is the least significant, has a weight of 20 (1). The next digit has a weight of 21 (2). The next has a weight of 22 (4), and so on.
Consider the binary number 11001010. This number is read as “one one zero zero one zero one zero.” This representation can get tedious after only a few digits and isn't practical in our everyday decimal world. Therefore, we typically convert binary numbers to decimal numbers.
Routers, being computers, work in binary. For example, 32-bit IPv4 addresses are used throughout the Internet, so you need to understand how to work with IP addresses. Because people usually like to work in decimal, IP addresses are typically written in a format called dotted decimal notation. The 32 bits in the address are divided into four 8-bit chunks—these chunks are called octets. Each octet is converted into decimal and then separated by dots.
When converting a binary octet to decimal, each binary digit is weighted based on its position, as described earlier. The weights for the eight bit positions in an octet are shown in Figure C-2.
For example, consider the following IP address, written in binary:
10101100000100001000001100001100
Follow these steps to write this binary address in dotted decimal notation:
Divide the 32 bits into four octets, as follows:
10101100 00010000 10000011 00001100
Convert the first octet into decimal, as illustrated in Figure C-3.
Note the following points in Figure C-3:
The least significant digit, 0, has a weight of 20; thus, it represents 0 * 20 = 0 * 1 = 0.
The next digit, also 0, has a weight of 21; thus, it represents 0 x* 21 = 0 * 2 = 0.
The next digit, 1, has a weight of 22; thus, it represents 1 * 22 = 1 * 4 = 4. The weighted value for each of the other digits is calculated in the same way.
The most significant digit (on the far left), 1, has a weight of 27; thus, it represents 1 * 27 = 1 * 128 = 128.
Therefore, the binary number 10101100 in decimal is the sum of each of these representations: 128 + 0 + 32 + 0 + 8 + 4 + 0 + 0 = 172.
Repeat this process for the other three octets. The results are as follows:
00010000 binary equals 16 decimal.
10000011 binary equals 131 decimal.
00001100 binary equals 12 decimal.
You can confirm these results using the decimal-to-binary conversion chart in Table C-1.
Thus, the IP address 10101100000100001000001100001100 in dotted decimal notation is 172.16.131.12.
To convert from decimal to binary, reverse the above process, taking each octet's decimal value and converting it to binary, as follows:
Referring to the weighting factors in Figure C-2, find the largest weighting factor that is smaller than or equal to the value of the octet. The binary number will have a 1 in the corresponding bit position and will have a 0 in any bit positions to the left of this 1.
Subtract that weighting factor from the decimal octet value.
Repeat this process using the new octet value, until the result is 0.
Set any remaining bit positions in the binary number to 0.
For example, consider the IP address 192.168.19.255. To write this address in binary, follow these steps:
Select an octet to convert. The conversion of the second octet, 168, to binary is shown in Figure C-4.
Figure C-4 describes the following points:
The largest weighting factor that is smaller than or equal to 168 is 128, so the binary number has a 1 in the 27 (128) position.
Subtracting 128 from 168 results in 168 – 128 = 40.
The largest weighting factor that is smaller than or equal to 40 is 32, so the binary number has a 1 in the 25 (32) position (with a 0 in the intermediary position).
Subtracting 32 from 40 results in 40 – 32 = 8.
The largest weighting factor that is smaller than or equal to 8 is 8, so the binary number has a 1 in the 23 (8) position (with a 0 in the intermediary position).
Subtracting 8 from 8 results in 8 – 8 = 0.
The remaining bit positions in the binary number are set to 0.
Thus, 168 in decimal equals 10101000 in binary.
Repeat this process for the other three octets. The results are as follows:
192 decimal equals 11000000 binary.
19 decimal equals 00010011 binary.
255 decimal equals 11111111 binary.
Again, you can confirm these results using the decimal-to-binary conversion chart in Table C-1.
Therefore, the IP address 192.168.19.255 in binary is as follows:
11000000 10101000 00010011 11111111