I have decided to publish this article again. I had posted this on the Internet about 4 years ago so that the students in my networking classes would have something to reference back to after I addressed the subject in class. I am going to post the article here again along with a video explaining IPv4 addressing (coming soon). I will post another article soon delving into the details of IPv6. This article here covers the decimal and binary math related to IP addressing and Subnet Masks. A well rounded knowledge of these concepts is critical for network engineers to know in order to design networks and implement security.
Ok, lets start out with a definitions of the terms. If you already know these, then good for you. These are my own definitions and formulas, not copied from anywhere. Please do not use these without giving credit, or gaining written consent.
TCP/IP: Transmission Control Protocol/Internet Protocol
Decimal: A numbering system with values from 0 to 9. Ten possible values. Also known as Base-10.
Binary: A numbering system from with values from 0 to 1. Two possible values. Also known as Base-2.
Bit: A single value in the binary system. Either a 1 or a 0.
Boolean Logic: A combinatorial system that uses the operators AND, OR, NOT, and XOR. Boolean logic can have one of two values true or false.
IP Address: The decimal representation of the binary value of the number that identifies a node on a network. A 32 bit number. I.E. 192.168.4.12
Broadcast: A network transmission that is sent to and processed by all the nodes on a subnet.
Multicast: A network transmission sent to and processed by group of nodes in a subnet.
Unicast: A network transmission sent to and processed by a specific node in a subnet.
Node: Anything that can have a IP address assigned to it.
Subnet: A mathematically segmented number of IP addresses that are on the same logical network (It does not have to be the same physical network, though it often is. Because of this it is sometimes confused with the ethernet contention domain.)
Broadcast Domain: All the IP addresses that receive the same broadcasts. Also a subnet.
Network ID: The decimal value of the ANDed IP address. Also the base value of the subnet. Always has the value of all 0s in the Host ID portion of the IP address.
Host ID: The portion of the IP address as defined by the subnet mask that identifies the node in the subnet.
Broadcast ID: The last IP address in a subnet. Cannot be assigned to a node. Is used for broadcast transmissions. Always the value of all 1s in the Host ID portion of the IP address.
Subnet Mask: A decimal representation of the amount of bits used to define the Network ID. I.E. 255.255.255.128
CIDR: Classless Inter Domain Routing. A shorthand method of defining a subnet mask. States the number of bits used to define the Network ID. I.E. 192.168.4.12/26 (the /26 is the CIDR notation)
Octet: A IP address is divided into four sections separated by decimal points. A section is called a octet. This is because it has eight binary bits.
Gateway IP address: The IP address that identifies the node that receives transmissions that are being sent to nodes that are not in the sending node’s subnet.
Integer: Whole numbers. Numbers without fraction or decimal. For example the integer of 2.67 would be 2.
Modulus: The simplest way to describe this is to say the remainder of a number divisable by another. In other words 8 mod 3 = 2. The remainder of 8 divided by 3 is 2.
Formula Variables:
A = Amount of Nodes
H = Host ID Bits
0 = Octet Value
N = Network ID
B = Broadcast ID
C = CIDR
U = Octet value counting from the left that is the value of “O”.
The math behind figuring out IP subnets is simple and can be done in your head as soon as you understand the concepts. It is just a conversion from binary to decimal. Below I have a chart with the math behind figuring the amount of nodes supported by a Subnet Mask. This covers subnets smaller than 255.255.255.0, but the math works the same for the larger subnets. Now let’s get into the math behind the madness. Let’s start with the formula for calculating the Octet (O) from the left that is the one to be used in the following Amount of Nodes equation, the formula is INT( C / 8 ) + 1 = U. This tells the Octet value, counted from the left that will populate O. Now to calculate the Amount of Nodes. Take 2 to the power of the amount of Host ID Bits multiplied by 256 to the power of 4 – U and then subtract a final 2 values for the Broadcast and Network ID since they are not addressable IPs, and the result will give you the amount of Nodes, so our formula is (2^H) * 256^(4-U) – 2 = A. Host ID bits is calculated by subtracting the CIDR from 32 (The amount of bits in a IP address) and a modulus of 8 (The amount of bits in a single Octet). So our formula for Host ID bits is (32 – C) mod 8 = H. So lets take the IP address and subnet of 192.168.12.129/26 and lets calculate the amount of nodes. We need to first get the Host ID bits so as in the previous equation we subtract the CIDR from 32 with a modulus of 8. That would be 32 – 26 which is 6, then 6 divided by 8 with the remainder of that division being 6, so our answer is 6 Host ID bits. Next we use the amount of Nodes formula (2^H) * 256^(4-U) – 2 = A. This would be (2^6) * 256^(4-4) – 2 = 62, or in words, 2 to the power of 6 multiplied by 256 to the power of 4 minus 4 and then a final 2 values are subtracted for N and B since they are not addressable and the result is 62, so 62 is the amount of Nodes we can have in that subnet.
Subnet Masks and their corresponding Node ranges:
|
Subnet Mask
|
CIDR
|
Host ID Bits
|
Math
|
|
255.255.255.0
|
/24
|
32-24=8
|
28 =
|
256
|
= 254 A + 1 B + 1 N |
|
255.255.255.128
|
/25
|
32-25=7
|
27 =
|
128
|
= 126 A + 1 B + 1 N |
|
255.255.255.192
|
/26
|
32-26=6
|
26 =
|
64
|
= 62 A + 1 B + 1 N |
|
255.255.255.224
|
/27
|
32-27=5
|
25 =
|
32
|
= 30 A + 1 B + 1 N |
|
255.255.255.240
|
/28
|
32-28=4
|
24 =
|
16
|
= 14 A + 1 B + 1 N |
|
255.255.255.248
|
/29
|
32-29=3
|
23 =
|
8
|
= 6 A + 1 B + 1 N |
|
255.255.255.252
|
/30
|
32-30=2
|
22 =
|
4
|
= 2 A + 1 B + 1 N |
|
255.255.255.254
|
/31
|
32-31=1
|
21 =
|
2
|
= 2 A |
|
255.255.255.255
|
/32
|
32-32=0
|
20 =
|
1
|
= 1 A |
Ok, now that we see that calculating the amount of Nodes is easy lets look at the formulas for calculating the Network IDs and Broadcast IDs. Lets use the same IP address of 192.168.12.129/26. The CIDR is 26 so remember our previous formula for calculating the Host ID bits (32 – C) mod 8 = H. So lets do it again, 32 – 26 mod 8 = 6 Host ID bits. Now for the formula for calculating the Octet (O) from the left that is the one to be used in the following Network ID equation, INT( C / 8 ) + 1 = U, so in this case since the CIDR or C value is 26 the formula would read like this INT( 26 / 8 ) + 1 = 4, or in words, the integer of 26 divided by 8 which is 3 plus 1 equals 4. That means that the Octet value to populate O will be 129, the 4th Octet from the left. Ok, now lets use the formula for calculating the Network ID (Int(O / 2^H)) * 2^H = N. Lets work through it, the integer of 129 divided by 2 to the power of 6 equals 2, or the integer of 129 divided by 64 equals 2. Then the rest of the formula is 2 multiplied by 2 to the power of 6, or 2 multiplied by 64 which equals 128, so our Network ID is 128. (Int(129 / 2^6))*2^6 = 128. Ok, now lets calculate the Broadcast ID. Now the entire formula for that would be (Int(O / 2^H))*2^H + (2^H – 1) = B, but since we already know the Network ID we can simple do N + (2^H – 1) = B. Lets work through it, 128 + (2^6 – 1) = 191, or in words, 128 plus 2 to the power of 6 which would be 128 plus 64 minus 1 and that equals 191. Ok then, our broadcast is 191. Lets see all the results of 192.168.12.129/26.
Network ID: 192.168.12.128
Broadcast ID: 192.168.12.191
Amount of Nodes: 62
Formulas:
H = (32 – C) mod 8 (Excel formula “H = mod( 32-C,8 )”)
A = (2^H) * 256^(4-U) – 2
U = INT( C / 8 ) + 1
N = (Int(O / 2^H))*2^H
B = (Int(O / 2^H))*2^H + (2^H – 1) or N + (2^H - 1)
These formulas work fine in Excel so you can simply copy and paste them in if you like. Obviously you will have to replace the variables with cell numbers, but that is a given.
So now you are going to say, how do I calculate the CIDR if all I am given is the subnet mask? I thought you might ask. I considered demonstrating this with a rather complicated equation, but I think a chart will simply be easier to use and remember. Remember that a subnet mask is simply the decimal representation of the amount of bits used by the Network ID. The boolean AND logic is used by the TCP/IP stack to AND the IP address against the subnet mask and the result is the Network ID. The subnet mask of 255.255.255.192 converted to binary would be 11111111.11111111.11111111.11000000. The CIDR is simply a count of the amount of bits from the left that are used to define the Network ID. If you notice the first, second, and third octets are all 1s since the binary value of 255 is 11111111, so all 8 of the bits are used for the Network ID in each of these octets. The last octet is 192 and the binary value of that is 11000000, so there are 2 bits used in this octet for the Network ID. So you can simply count the amount of 1s to get the CIDR or do 8 + 8 + 8 + 2 = 26 and that is the CIDR. To get the CIDR just use the chart below and add the bits for Network ID together to get the CIDR. I.E. The CIDR for 255.255.240.0 would be 8 + 8 + 4 = 20.
Subnet Mask Decimal and Binary Equivalents:
|
Binary
|
Decimal
|
Bits for Network ID
|
|
10000000
|
128
|
1
|
|
11000000
|
192
|
2
|
|
11100000
|
224
|
3
|
|
11110000
|
240
|
4
|
|
11111000
|
248
|
5
|
|
11111100
|
252
|
6
|
|
11111110
|
254
|
7
|
|
11111111
|
255
|
8
|
It has been my opinion for sometime now that if a network engineer is incapable of performing these simple functions in his head, then he should look for a job elsewhere. The reasoning behind that is if they are not able to perform this simple task what other essential networking tasks are they not capable of. General networking theory knowledge is so lacking today; sometimes I wonder how some people are capable of performing their jobs (Google is good). I have some fellow engineers that disagree with me on this opinion, you can guess who they are. :)
Sphere: Related Content