Skip to content

Instantly share code, notes, and snippets.

Last active December 28, 2020 17:43
Show Gist options
  • Save inadarei/86968c7e5b8f322db260e56723e105ec to your computer and use it in GitHub Desktop.
Save inadarei/86968c7e5b8f322db260e56723e105ec to your computer and use it in GitHub Desktop.
IP Address Classes, CIDRs and Network Masks

Network Classes

IP Addresses identify both a network as well as host on a network. Depending on the class of a network, certain amount of bits in the IP (32 bits overall) are allocated for network adressing and the rest is for: adressing the host on the network.

  1. Class A: first octet is network, last three: host
  2. Class B: first two octets are for network, last two: host
  3. Class C: first three octets are for network, only last octet is for host.

Furthermore, looking at the IP itself, you can tell which class of IP it is, using the following rules:

Class Binary Prefix First Octet of
dotted-decimal must be
Excluded Addresses
A 0 0 to 127 10.% and 127.%
B 10 128 to 191 172.16.% to 172.31.%
C 110 192 to 223 192.168.% to 192.168.%
1110 224 to 239
11110 240 to 254

Additional, Special IP Rules:

  1. All-zeros (in decimal) host is the network itself. E.g. - is the network 141.23
  2. All-ones (in decimal) is a broadcast address signaling to all hosts of the network, e.g. is a broadcast address for the 141.23 network
    • Please note that can also be used to broadcast on a current network.
  3. 127.% are loopback IPs
  4. RFC1597 reserves following ranges for private networks:
    1. 10.%
    2. 172.16.% - 172.31.%
    3. 192.168.% - 192.168.%
  5. Reserved for Autoconfiguration Range: 169.254.% to 169.%

Subnet Masks

IP address = {Networks bits} + {Host bits}. Subnets "borrow" bits from the host space, so how many subnets vs. hosts your mask allows depends on the network class.

E.g.: consider class A network, subnet mask: = first 8 bits are for network, subnet bits are: following 8 bits from the second octet + 5 bits (11111000 = 248) from the third octet, host: remaining 3 bits from third octet + 8 bits of fourth octet. In theory this gives 2^13 = 8192 subnets and 2^11 = 2048 hosts, except all 1's and all 0's IPs are not allowed. So, final result: 8190 subnets and 2046 hosts per subnet.

Same mask for a class B network will lead to different result. Since first two octets are for network address, the subnet + host space is only last two octets. Therefore, subnets only get 5 bits of the third octet = 2^5-2=30 subnets. You still get 2046 hosts per subnet.

Classless Inter-Domain Routing (CIDR)

CIDR notation indicates how many bits are taken in the mask by network + subnet. Remembering that overall there are 32 bits, the remaining bits can be used for host addresses. For instance: /25 indicates that 32-25 = 7 bits are available for the host portion, making 2^7=128 (in reality 128-2 = 126) host addresses available for assignment. The full CIDR address includes an IP (usually: lowest IP) of the range, e.g.:

Example CIDR -> IP Range Calculation.



  1. Figure-out netmask.

    Since 32-22 = 10, first two octets are all 1's, last one is all 0's and first six bits of the third octet are 1's: 11111111 11111111 11111100 00000000. Calculating the equivalent decimal number for the third octet: 128+64+32+16+8+4 = 252, the final netmask is: Obviously, here we remembered bit weights: 128,64,32,16,8,4,2,1 by heart, but if you don't, you just need to remember 128 is the high bit and the rest are sequence of dividing by 2 until 1.

  2. Calculate IP range of the range.

    Knowing our netmask, we know that the lowest IP will have the same number in the first two octets as the example IP: 212.100.%. Since last octet is not masked, lowest IP will have it as zero, so we just need to figure-out lowest allowed number in the third octet. Third octet (252) in binary is: '1111 - 1100' which would enforce that the first six bits of the example IP are the same for all IPs in range. 105 in binary is: '0110 - 1001', therefore the lowest number with fixed six bits is: '0110-1000' (64+32+8=104) and highest is: '0110 - 1011' (64+32+8+2+1=107), where bolded-out part is the fixed part. Considering this, the final range is: -

There're also some really nice online calculators for these, e.g.:

Please note: as a practical matter most commonly, CIDR ranges do indicate the lowest IP, since lowest IP is the network IP and the highest is broadcast IP, for that network. However, as we saw - we can calculate the proper range even if any other IP from the range is given (possibly: by mistake).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment