IP
Addressing:
Understanding Binary,
Hexadecimals, and Powers of 2
By: Scott R. Lenz; October 1,
2002
The Powers of 2
Understanding how to convert IP addresses to their Binary and Hexadecimal equivalents are helpful when dealing with IP addressing schemes and most especially when dealing with subnetting. Knowing the powers of 2 is the best way to start.
An IP address is made up of 4 octets, an octet being a 1 byte number, 1 byte = 8 bits. Therefore, you can say an IP address is a 32 bit number.
A bit is simply a 1 or a 0, nothing more, nothing less. Look at it as an ON or an OFF state. 1 being ON and 0 being OFF. An 8 bit number (a byte) is a sequence of 1's and zeros that, when using power of 2 translation, becomes a number by turning ON or OFF it's corresponding hierarchical bit, which is a hierarchical power of 2. When breaking down a byte to bits, you will want to look at the number as 2 groups of 4 bits, as opposed to 1 large group of 8 bits. This is to help with translating IP addresses to binary and hexadecimal values. You'll see why in a bit.
The maximum value an octet will have will be 255, which actually carries a range of 256 possible values, as 0 is included as a value (0 - 255 = 256 values).
table bh1.1: The Powers of 2 table for an octet
128
64
32
16
8
4
2
1
27
26
25
24
23
22
21
20
Keep in mind that we are focusing on octets for IP schemes. A binary number can actually exceed values of 255, although, not when your dealing with IP addresses. Remember, an IP address is made up of 4 bytes (4 octets), which are 4 8-bit numbers for a total of 32 bits for a standard ip address. An 8 bit (1 byte) number cannot exceed a max value of 255 (which is a span of 256 values, remember that 0 is also a value!!!) While not applicable to IP addressing, I'll touch a bit more on working with binaries greater than 255 later.
In table bh1.1, the top row contains 8 values. Each of these values, when read from right to left, is a power of 2, as shown by the second row. A Power is a number value multiplied by itself X number of times, where X = the superscripted numeric value attached to the primary value. A value of 27 means 2 x 2 x 2 x 2 x 2 x 2 x 2. Each of the values in the top row, as related to a binary number, represents a single bit. By turning the bits on and off, you can attain values up to 255 in an octet. The reason we split the octet into 2 groups of 4 bits is for explaining hexadecimal notation a bit later.
Converting Decimals to Binarys
Now, look at table bh1.2.
table bh1.2: The number 233 in binary
128
64
32
16
8
4
2
1
1
1
1
0
1
0
0
1
Based on this, the binary representation for 233 is 11101001. It is read as 128+64+32+8+1=233. So, how do we get this? Assuming that your not of the mindset where such calculations pop easily into your head, there is an easy way to do this. First, write down the number 233. Then, below it, write down the half of 233, ignoring any remainder. Complete this process until you reach a value of 1. Your result would look like this:
233
116
58
29
14
7
3
1
Next, in a column to the right of your numbers, you'll want to put in 0's and 1's. If the value in the left column is an ODD value, put a 1 in the right hand column. If the left value is EVEN, then the right value is 0. Look at table BH1.3 to see the result.
table bh1.3: Calculating 233 into binary
233
1
116
0
58
0
29
1
14
0
7
1
3
1
1
1
Next, you write down the right hand column going from . You get 11101001. As you can see, this matches what we see in table bh1.2. One thing to remember is that your binary value MUST be in blocks of 4. If you get a binary result that is not a length of 4 or 8 (or 12, 16, 20, ... etc, but only if your not dealing with an IP address), then add 0's to the beginning of your binary number to format it to a length that is divisable by 4. When dealing with IP addresses, this most likely will not happen. Go ahead and try this for yourself a few times. Try converting the numbers 141, 54, 251, 22, and 111 to binary. I'll include the answers below, but use the above method to see if you can get the same. To see the answers, just highlight the answer field in your browser by clicking on the respective cell, holding down your left mouse button, and dragging the mouse across the length of the cell.
141 |
10001101 |
54 |
00110110 |
251 |
11111011 |
22 |
00010110 |
111 |
01101111 |
Converting from Binary to Decimal
Now that you understand how to convert a decimal value into a binary expression, we must look at how we can reverse the process. Lets take the binary expression 11011011. Unless you think in binary (most likely not), you won't immediately recognize the decimal value for this. To convert this expression, create a 2 column table with the number of rows equal to the length of the expression + 1. When dealing with IP address octets, this will always be a 9 row table. If your dealing with non-IP related binary values, the table can be longer or shorter (although, usually not longer than 65, which is a 64 bit binary + 1.) In the top left cell, place a 0. In the right cells, starting with the second cell down, place each of the bits top down whe reading the expression from left to right. Look at the table we create in table bh1.4
table bh1.4: Table for converting binary expressions to a decimal value
0
1
1
0
1
1
0
1
1
Now, starting with the second left cell, place the double of the value above it. If the value to the right (the binary expression column) is a 0, simply double the left value. If the value in the binary expression column is a 1, double the value and add a 1 to the result. Since our first number is a 0, we will double the 0 (still getting a 0!) and add 1, since our first binary expression value is a 1. Thus, our second leftmost cell will contain a value of 1. It is possible that you may get a number of zeros traveling down the left column before you start getting non-zero values IF the binary expression column contains a number of leading zeros as well. This is okay. It is how it should be. Our third left cell will double the value above it, that value being 1, to get 2, then adding the binary expression value to then right of it to get 3. table bh1.5 shows our completed table.
table bh1.5: Completed conversion table
0 |
|
1 (0x2+1) |
1 |
3 (1x2+1) |
1 |
6 (3x2+0) |
0 |
13 (6x2+1) |
1 |
27 (13x2+1) |
1 |
54 (27x2+0) |
0 |
109 (54x2+1) |
1 |
219 (109x2+1) |
1 |
When the table is completed, your decimal value in the bottom left cell is, TAA-DAA!, your decimal value (219) for the binary expression 11011011. So simple it should almost be illegal, no? Try converting the following binary expressions into decimal values yourself [ 10011111, 00101011, 10101100, 11101010, 01000100]. Once again, I'll include the answers, but use the conversion table method I just showed you to see if you can get the same results. To see the answers, just highlight the answer field in your browser by clicking on the respective cell, holding down your left mouse button, and dragging the mouse across the length of the cell.
10011111
159
00101011
43
10101100
172
11101010
234
01000100
68
The Big Picture (32 bit addresses)
So, know that you know how to convert a single octet (1 byte or 8-bit) number, how does that relate to a full, 4 octet (4 byte or 32 bit) IP address. Luckily, IP addresses follow a standard addressing scheme, which allows for identifying octets when using decimal values or binary expressions. I'll show a few examples of decimal value IP addresses and their binary equivalents.
204.133.131.2 |
11001100.10000101.10000011.00000010 |
198.176.24.212 |
11000110.10110000.00011000.11010100 |
201.139.6.22 |
11001001.10001011.00000110.00010110 |
5.139.16.20 |
00000101.10001011.00010000.00010100 |
Notice how each binary octet is broken up by a decimal ( a 'dot' in geek speak,) just like the decimal octets are separated. Even so, if the binary expression did not contain decimals, you could still figure out the octets, simply by breaking the number by grouping the bits into 8 character groups, starting from right to left. If the number of bits is not divisable by 8, don't worry! You didn't neccessarily get an invalid IP address. Just add 0's to the left hand side of the expression until it's length is divisable by eight (although, this will never happen if your given an IP address from an originating automated source like a router or through a protocol analyzer).
Knowing the above sections and methods are key to understanding subnetting of Class A, B, and C networks. If you feel comfortable with binary to decimal to binary conversion methods, you can go on to my Subnetting IP Networks tutorial right away. However, I recommend you read through the following section on Hexadecimal notation. Hexadecimal notation is used in numerous places in regards to networking technologies (especially with MAC addresses). Another link to my Subnetting IP Networks tutorial will also be included at the end of that section.
Converting Binarys and Decimals to Hexadecimals
Okay, so know you know how to convert IP address octets from decimal values to binary expressions and back again. So, what if someone hands you the following IP address:
CC 85 83 02
Will you just start at them blankly like they are insane? No, instead, you will tell them that IP address 204.133.131.2 will be handled as per their request. How did you know that the series of numbers and letters equaled that IP address. Because you read the following section, thats why! Keep in mind that getting an IP address in it's hexadecimal format is not exactly a common occurance. Mostly you will see it applied with how Windows stores IP addresses in it's registry files, among other examples. But it will happen, and so, you should familiarize yourself with the following section.
It's important to note that when converting decimal values to their hexadecimal equivalent, or vice versa, you must first translate the value to it's binary expression. So make sure you understand the above sections on binary conversion before continuing. With that in mind, take a look (and memorize!!) the following table:
table bh1.6: Hexadecimal character equivalents to 4 bit binary expressions
0000
0
0001
1
0010
2
0011
3
0100
4
0101
5
0110
6
0111
7
1000
8
1001
9
1010
A
1011
B
1100
C
1101
D
1110
E
1111
F
As you can see, a hexadecimal expression character range is from 0 to 15 (for 16 possible values). Thus, a hex character is comprised of 4 bits. However, a valid Hexadecimal address is comprised of 8 bits (or a byte). A 4 bit expression contains a maximum value of 16, which is the same as a single Hexadecimal character. Thus, you can see that a single hexadecimal character is equal to a 4 bit value. However, you cannot have a hexadecimal value of A, or 7 ( 0A and 07 are valid, though! ), it must be a 2 character minimum expression such as FA or 4C. Remember when I told you to look at binary addresses in 4 bit sections back at the beginning of this tutorial? This is why. To convert a byte (an 8 bit expression) into a hexadecimal value, you first break the binary expression of the byte (10100111 for example) into 2, 4 bit expressions. Thus, our example would be broken into [1010] & [0111]. Once again, this is in dealing with IP addressing schemes. You can have valid 3 or more character hexadecimal addresses, but not in regards to IP addresses.
Now, with our example binary expression (10100111) broken down into 1010 and 0111, you can easily convert it to a hexadecimal address by referring to the above chart. 1010 is the equivalent 4 bit address of hexadecimal character A. The expression 0111 is the same as a Hexadecimal character 7. Thus, the binary expression 10100111 is the same as the Hexadecimal expression A7. Seems pretty simple, doesn't it?
So, say you want to convert the IP address 204.133.131.2 into hexadecimal. How would you do it? First, you would convert each Octet into it's binary expression equivalent. Then, you would take each 8-bit binary expression and separate it into 2 4-bit expressions. You would then convert each 4 digit expression into it's hexadecimal equivalent. Take a look at table bh1.7.
table bh1.7: Converting a decimal IP address into a hexadecimal expression
204
133
131
2
11001100
.10000101
.10000011
.00000010
1100
1100
1000
0101
1000
0011
0000
0010
C
C
8
5
8
3
0
2
What we did: We first took each octet of our decimal IP address (Row 1) and converted them into their respective 8-bit binary expression (Row 2). We then split each 8-bit expression into 2, 4-bit expressions (Row 3). Then, using the chart in table bh1.6, we converted each 4-bit expresion into it's hexadecimal equivalent. Thus, 204.133.131.2 is equal to the hexadecimal expression: CC 85 83 02!
To convert a Hexadecimal expression back into it's decimal equivalent, you simply reverse the process. Hexadecimal isn't tough, the key is understanding how to convert decimal values into binary expressions, and vice versa. Converting the binary expression into a Hexadecimal value is then a simple case of refering to Table bh1.6 (or, doing it in your head since you followed my advice and memorized the value expressions!)
Okay, try it yourself. I'll show you a few Decimal notated IP addresses, and a few Hex notated ones in the fields below. Try to convert them using the methods you learned in this tutorial. I'll also, again, include (but hide!) the answers. To see the answers, just highlight the answer field in your browser by clicking on the respective cell, holding down your left mouse button, and dragging the mouse across the length of the cell.
209.138.16.243
D1 8A 10 F3
176.111.13.42
B0 6F 0D 2A
219.139.10.2
DB 8B 8A 02
C7 C5 10 FC
199.197.16.252
B2 38 A4 16
178.56.164.22
07 13 16 01
7.19.22.1