Home » Networking » Calculating Ip Addresses And Subnets

Calculating IP Addresses and Subnets – A Reference

This article aims to familiarise you with IP addresses and subnets and the calculations involved when setting them up. It’s a big topic, so this article will try to stay focused on its practical side to help get you on the right track and subnetting your home network with ease.

Networking and Subnets

Let’s skip the analogies and break it straight down:

An IP address contains two components the network address describing the network and the host address describing the host on the network.

  • Networks can be split into subnets – sub-networks
  • The subnet mask (AKA netmask) defines the range of IP addresses available within a (sub) network.
  • Hosts in the same subnet can communicate directly.
  • Hosts on different subnets must communicate via a router.
  • The subnet mask determines, in an IP address, which part identifies the network and which part identifies the host (or device) on the network.
    • It’s called a ‘mask’ because it ‘masks’ the network part of the IP address, leaving the host address.

Each subnet contains at least two addresses:

  • Network address
    • Usually, the first address in the range.
    • Identifies the network itself – devices use it to recognize different networks.
  • Broadcast address
    • Usually, the last address in the range.
    • Traffic to this address goes to all hosts on the network.

IPv4

IPv4 (Internet Protocol version 4) is the standard used for computers to communicate on a network (and the Internet).

An IPv4 address looks like this:

192.168.1.5

It consists of 4 sets of up to 3 numbers referred to as OCTETS from 0-255 (inclusive) separated by a period (.).

They’re called octets as each set consists of 8 bits when expressed in binary – see the table later in the article for a visual representation.

This gives a total of 4,294,967,296 possible addresses – a supply that has rapidly dwindled as more and more people get online, requiring Internet IP addresses for their devices. Many tricks are being used to keep the supply available, like having devices share addresses behind routers, but it’s only a matter of time before the addresses run out.

Due to this, IPv4 is slowly being replaced out on the Internet. Though it is still the standard, when IPv4 addresses run out, they’ll run out, and IPv6 will reign.

However, IPv4 remains popular and will remain popular on local networks in homes and offices, as:

  • It is easy to read and calculate network addresses/subnets without a special calculator.
  • Legacy device support (many only support IPv4) – things like office printers, Point of Sales hardware, etc
  • It’s way easier to remember an IPv4 address for a few minutes while you run from one side of the office to another than to remember an IPv6 address!

Subnet Masks

Here’s an IPv4 subnet mask, commonly used on home networks:

255.255.255.0

It has the same format as a standard IPv4 address. This subnet mask is for a class C subnet, as shown in the below table.

For the netmask 255.255.255.0, the first three octets have a value of 255, which determines that the IP addresses for hosts within the subnet will share the first 3 octets. This means that the first 3 sets of numbers are the network IP address, and the last set/octet is the host IP address – allowing for 256 unique hosts (254 subtracting the network and broadcast addresses).

Why is this? Check out the table below, which is hopefully the only binary we’ll need to explain things:

255 255 255 0
11111111 11111111 11111111 00000000

Remember this for later when we look at CIDR Notation! 8 + 8 + 8 = 24

This table shows the binary representation of each octet. You can see that 255 results in all of the binary values in the octets being set to 1, which denotes that they are part of the network address. The leftover bits (0s) are thus the available host IP addresses.

Here’s a second octet table to further illustrate:

255 255 128 0
11111111 11111111 10000000 00000000

Remember this for later when we look at CIDR Notation! 8 + 8 + 1 = 17

Again, this table shows the binary representation of each octet. The third octet is set to 128 rather than 255, meaning that part of that octet is available for host addresses as well as the final octet.

CIDR Notation

CIDR (Classless Inter-domain Routing) notation is a shorthand way of describing an IP address and its subnet information. It looks like this:

192.168.1.5/24
  • Everything before the forward slash is the IP address.
  • The number after the slash is a decimal number suffix describing the network mask.

Remember 8 + 8 + 8 = 24 from above? That’s how we calculated this decimal suffix – it’s the sum of the binary representation of the netmask

Thus, the decimal suffixes in CIDR notation range from 0 to 32.

This means you can represent IP ranges with as few as 2 addresses to as many as 232 addresses.

Public and Private Addresses, Subnet Classes

IPv4 is split into several classes – the classes are big subnets described in this table:

Class Subnet mask Number of addresses First octet Example address
Class A Subnet 255.0.0.0 16,777,216 Between 1 and 126, inclusive 10.52.36.11
Class B Subnet 255.255.0.0 1,048,576 Between 128 and 191, inclusive 172.16.52.63
Class C Subnet 255.255.255.0 65,536 Between 192 and 223, inclusive 192.168.1.5

These ranges are considered private address space (for use on home or office networks, with no public access), and everything outside of them is considered public address space (the Internet!).

Your home WiFi network devices will all have an address in a range defined in one of these classes. When a device requests data from the Internet, your router will have an external address in the public range, and your router will translate traffic from the public internet to the device on your private network, which made the request.

Subnetting in IPv4 – Practical Examples

We will focus on subnetting with private addresses. If you’re working in the public address space, you should probably pick up some heavier reading on the topic.

Why would you want to subnet your home network?

  • Split up your network – keep your home entertainment devices separate from your mission-critical work devices.
  • Keep the kids on their own network so they can’t access your stuff.
  • Or do the opposite – fit more devices on your network by setting up a larger than default subnet.

The Quick Method For Calculating Subnets and IP Addresses

Just use this table – it summarizes the IP address counts, netmasks, and CIDR notations! You can manually count binary bits and do these calculations yourself, but who actually wants to do that?

CIDR notation Available IP Addresses IP Addresses Available to Hosts Netmask
/30 4 2 255.255.255.252
/29 8 6 255.255.255.248
/28 16 14 255.255.255.240
/27 32 30 255.255.255.224
/26 64 62 255.255.255.192
/25 128 126 255.255.255.128
/24 256 254 255.255.255.0
/23 512 510 255.255.254.0
/22 1,024 1,022 255.255.252.0
/21 2,048 2,046 255.255.248.0
/20 4,096 4,094 255.255.240.0
/19 8,192 8,190 255.255.224.0
/18 16,384 16,382 255.255.192.0
/17 32,768 32,766 255.255.128.0
/16 65,536 65,534 255.255.0.0
/15 131,072 131,070 255.254.0.0
/14 262,144 262,142 255.252.0.0
/13 524,288 524,286 255.248.0.0
/12 1,048,576 1,048,574 255.240.0.0
/11 2,097,152 2,097,150 255.224.0.0
/10 4,194,304 4,194,302 255.192.0.0
/9 8,388,608 8,388,606 255.128.0.0
/8 16,777,216 16,777,214 255.0.0.0
/7 33,554,432 33,554,430 254.0.0.0
/6 67,108,864 67,108,862 252.0.0.0
/5 134,217,728 134,217,726 248.0.0.0
/4 268,435,456 268,435,454 240.0.0.0
/3 536,870,912 536,870,910 224.0.0.0
/2 1,073,741,824 1,073,741,822 192.0.0.0
/1 2,147,483,648 2,147,483,646 128.0.0.0
/0 4,294,967,296 4,294,967,294 0.0.0.0

You’ll notice that subnets all have a number of IP addresses that are a power of 2 – You can’t, for example, have a subnet with 7 IP addresses

Here’s how to use the table with some real-world examples.

Example 1

  • A network in the Class C range
  • 16 IP addresses
  • Including network and broadcast addresses

So let’s build a network with:

  • IP range of 192.168.1.0 to 192.168.1.15 (16 total addresses as we start with 0)

By referring to the above table and finding the row with 16 available addresses, and taking the CIDR and netmask, we find that we will need:

  • Subnet mask of 255.255.255.240
  • CIDR of 192.168.1.0/28

This network will have:

  • The network address of 192.168.1.0
  • The broadcast address of 192.168.1.15
  • 14 IP addresses left for hosts to use

Example 2

  • A network in the Class A range
  • 512 IP addresses
  • Including network and broadcast addresses

So let’s build a network with:

  • IP range of 10.1.0.0 to 10.1.1.255 (512 total addresses – as the last octet can hold 256 unique values, and the second to last octet will need to have two unique values, so that multiplied they create 512 possible unique IP addresses)

By referring to the above table and finding the row with 512 available addresses, and taking the CIDR and netmask, we find that we will need:

  • Subnet mask of 255.255.254.0
  • CIDR of 10.1.0.0/23

This network will have:

  • The network address of 10.1.0.0
  • The broadcast address of 10.1.1.255
  • 510 IP addresses left for hosts to use

IPv6

IPv6 is the solution to the dwindling number of addresses used for IPv4. An IPv6 address looks like this:

2001:0db8:0000:0000:0000:ff00:0042:8329

It consists of 8 sets of 4 hexadecimal digits separated by a colon (:). It allows for an unfathomable number of addresses. We will not look at subnetting for IPv6 here – it’s just easier to use a special IPv6 subnet calculator, and IPv6 is yet to supersede IPv4 on local networks.

Conclusion

There are whole books dedicated to IP addressing and networking if you want to fully grok what’s going on – if you’re working on mission-critical networks, a full understanding of the underpinnings of network and even IPv6 is probably a good thing to have.

The goal of this article was to distill the basics without getting too bogged down, a quick read to get you jump-started on IP networking – hopefully, no meaning was lost in brevity.

Check out our other networking articles!

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment