# CIDR Notation Host Count

Late last year, I spent some time studying for the Amazon Web Services (AWS) Solutions Architect – Associate exam. In doing so, I briefly ended up covering CIDR again and the two-step math problem required to determine the number of available hosts in a CIDR IP address range.

As a recap, it works this way. Let’s consider the largest VPC (Virtual Private Cloud), or a virtual network, one can define in AWS. That’s 10.0.0.0/16. The way to determine the number of available host IPs is to subtract 16 (as indicated by the /16) from 32 — a constant value. In this example, that total is also 16. We then raise 2 (the base — another constant) to the power of 16 (the exponent/our difference), which results in 65,536 possible hosts. Because PowerShell can often distract me from AWS, let’s take a look at a small function I quickly wrote out — yes, during my AWS study time — to do the conversion for me.

When invoked, the above Get-CidrHostCount function will accept any numeric value from 1 through 32, and determine how many hosts the CIDR range would allow. This 1 to 32 value is equivalent to the number after the forward slash in the CIDR notation. Do notice our two-step math problem. The System namespace’s Math class includes a method called Pow. This method accepts two values. The first value is our base again — 2 — and the second number is the exponent. In the function, we use the constant value of 32 and subtract the CIDR value that’s passed in when the function is invoked. These two values are then used to complete the calculation.

The below examples display a few results, but eventually return all the possible results.

Before we really wrap it up here, let’s change the results as many of us would prefer to see them. Here’s a mildly modified version of the function and the last above command run again.

And that’s it — numbers I can actually read. And yes, I finally did get back to studying for my exam.

IT Pro. Passionate for #PowerShell, #AWS (certified x2), & all things automation. I'm not done learning. Author in #PSConfBook. Writes at https://powershell.org.

## 2 thoughts on “CIDR Notation Host Count”

1. Matt Bloomfield

Although it gives the number of IP addresses for the CIDR, it doesn’t really give you the number of hosts. In normal networking the number of hosts will be 2^H-2 because you lose one IP for the network address and one for the broadcast address.

AWS has different rules and there are five addresses that can’t be used: the first four and the last one. So for AWS the formula is 2^H-5.

In both cases H = number of host bits (32 – number of subnet bits or CIDR prefix).

1. Tommy Maynard (tommymaynard) Post author

Hi Matt — thank you for your comment.

I appreciate that you’ve clarified the distinction between the host count and available IP address count. These are absolutely two different things.

As Matt mentioned, in a traditional network the first and last IP addresses are reserved as the network address and broadcast address, respectively. He’s also correct that inside an Amazon VPC, the first four and last available IP addresses are reserved. In order, these are for the network, the VPC router, DNS, AWS future use, and broadcast.

Thanks again, Matt!