**Calculate IPv4 Subnets**

One thing that trips up a lot of network administrators is properly calculating subnets outside of a typical "Class C" (which has a subnet mask of 255.255.255.0 and 256 addresses in the subnet).

Of course, you could use the Spiceworks subnet calculator, but sometimes you don't have such a utility available. Besides, that particular calculator has some limitations, like the inability to specify anything larger than a Class C.

Calculating IPv4 subnets can actually be quite easy, and you don't even need to use binary math. You probably won't even need a calculator or a special program. If you learn the principles I present below, you'll be able to do it in your head.

**IPv4 Address Format**

IPv4 addresses are made up of four octects separated by periods, or dots. An example is 207.200.53.187 -- one of the addresses that www.spiceworks.com resolves to. The numbers in the address are called octects because they are the decimal values of 8-bit binary words. Each binary digit (bit) represents a power of two.

The rightmost bit represents a value of 2^0 (1) and the leftmost bit a value of 2^7 (128). If all bits have a binary value of 1, then the value of the 8-bit word is 255. Here is a handy reference for the value of each bit:

2^0 = 12^1 = 2

2^2 = 4

2^3 = 8

2^4 = 16

2^5 = 32

2^6 = 64

2^7 = 128

2^8 = 256

(Yes, "two to the zeroth power equals one" is a legitimate mathematical construct. For that matter, "anything to the zeroth power" always equals one. Also note that there are 9 values above; I included 2^8 as that is useful to know as well.)

Calculating powers of 2 is easy: each integer increase in the power doubles the result. 1+1=2, 2+2=4, 4+4=8, 8+8=16, and so on. The total number of addresses in a subnet must always be a power of 2.

**How many addresses in the subnet?**

Since each octet of an IPv4 subnet has 256 possible values (0-255), 256 is a very important number and forms the basis for the rest of the math. We'll start with an easy question: how many addresses in a subnet if the mask is 255.255.255.248? We will ignore the first three octets for now and look at the last. Here is how easy it is: subtract 248 from 256. 256 minus 248 equals 8. There are 8 addresses available (including the network and broadcast addresses). The reverse also works: if I want to have a subnet with 16 addresses, what will the subnet mask be? 256 minus 16 equals 240. The subnet mask will be 255.255.255.240.

Now if we want to expand beyond a "class C" scope, it gets only a tiny bit more complicated: if our last octet is 0 and our third octet is, say, 240, then we do the math on the third octet and find that there would be 16 addresses. So we multiply 16 by 256 (the number of addresses in the last octet) to get 4,096. If both the last two octets are 0, then we take the subtraction result from the second octet (we'll say it's 16 again), multiply but 256 (addresses in the third octet), multiply again by 256 (addresses in the last octet) to get 1,048,576 addresses. Easy as that! (OK, so the reverse is a little more difficult. If we want a subnet with 1,048,576 addresses, we'll have to divide that number by 256 a couple of times to get a number we can subtract from 256.)

**Network Address and Broadcast Address**

Now that we know how to calculate the subnet mask, how do we figure out what the network address is? That's easy: it's always a multiple of the number of addresses in our subnet. So if we have 16 addresses in our subnet, the possible network addresses will be 0 (yes, 0 is a multiple of 16), 16, 32, 48, 64, and so on up to 240.

Where the network address is the first address in the subnet, the broadcast address is always the last address in the subnet.

**CIDR Notation**

So how about CIDR notation? How do translate that to and from an IPv4 address?

Remember our powers of two? Well, now we have another key number to remember besides 256: 32. Remember, CIDR notation describes the number of significant bits in the IPv4 address (where the significant bits are the leftmost, or unchanging bits in the subnet), and there are 32 bits in an IPv4 address, 8 for each octet. So if we have a subnet mask of 255.255.255.240, that is 16 addresses. If we look at our "powers of 2" table above, we see that 16 is two to the fourth power (2^4). So we subtract that power number -- 4 -- from 32 and get 28. Our CIDR notation for a subnet mask of 255.255.255.240, our CIDR notation is <network address>/28. And if we are given a /28, we subtract that (28) from 32 to get 4; raise 2 to that (4th) power (2^4) to get 16; then subtract that (16) from 256 to get 240; or 255.255.255.240.

How easy is that? Except for the "very large value of 2" (1,048,576), I did all that math IN MY HEAD AS I WAS TYPING THIS.

**Epilogue: IPv6**

You may wonder, if IPv6 is just around the corner, why one would bother learning IPv4. It is wise to learn IPv4 because the installed base of IPv4 devices is so great, that it will be around for a long while, yet. Being able to manage legacy systems is a valuable skill.

P.S. -- I'm not sure how to do superscript in a Spiceworks post. Any advice?Edited Sep 27, 2016 at 19:56 UTC

## 35 Replies

NetAdminWorld is an IT service provider.

Heck, I used to be able to do subnets in my sleep; then is then, now is now. Nice refresher, thanks.

About superscripts in SW posts, unless you have access to the code for the posts, I don't think you can.

Ironically, they use IS-IS routing protocol to connect their caves and militant sites.I'm a networking nerd, not a graphics artist. If ISIS ever kidnaps me, I hope they never ask me to polish up their website!What, no pictures?!

;0)

Ha! Now that would be a client you wouldn't want to say no to.

Best explanation and math I've seen. My instructor in school couldn't touch it.

Then again, I had a supervisor once that got some instruction that said "we need the PCs networked like this: 192.168.9.0/24" She told my colleague that we needed 24 PCs with that address. I said, no that's notation indication a 255.255.255.0 subnet mask. Good times, good times.

All real-world math is word problems. Nobody in real life hands you an equation and says "solve this." They hand you a problem (in words), and you have to figure out the equation.My head is spinning, the only reason I do not have any Cisco certs is because of this. I have always been good at math but sucked at word problems, there are too many words in there for me to extract the data I need to understand this.

```
How easy is that?
```

It would be easier with pictures!

Seriously, though. This is a great article. Now that I read it, it seems so freaking obvious I can't believe I didn't figure it out myself. Subneting and subnet math has always made me want to slam my fingers in my car door. I use it daily and this will be a huge help. Thanks!

IWR Consultancy is an IT service provider.

Wouldn't that be Arabic, so I guess you'd have to explain it in PTR record terms. If ISIS kidnaps you, just describe to them how to calculate IPv4 subnets in their head. When they start to fall asleep, bail out.