# Simple way for IPV4 subnet calculation

### Overview

Many people think subneting in IPV4 address is a difficult to calculate. But in this tutorial I'll show you the simple technique to solve the IPV4 calculation. Before start please read and understand few thing as below:

IPV4 address contain 32bit,  Ex: 192.168.0.1.
192 contain 8bit, if we calculate to binary it equal 11000000, 168 has 8bit, 0 has 8bit, 1 also has 8bit. So we combine togather is 32bit

Subnet, prefix length define the size of network. It also contain 32bit number. 0 is hostbit and 1 is network bit.
Ex: 255.255.255.0. 255=11111111, So equal 11111111.11111111.11111111.00000000
The prefix length is number of network bit. So prefix length is "/24"

Broadcast address is used to send packet to all node inside it's network.
Ex: 192.168.0.0/24.  24bit is Network ID (192.168.0) and 8bit is Host ID (0). The last IP of the network range is broadcase address.
Number of IP = 2^(host bit) = 2^8 = 256 IPs
IP start from 192.168.0.0 – 192.168.0.255

Network address is used for identify network segment.
Ex: 192.168.0.0/24.  24bit is Network ID (192.168.0) and 8bit is Host ID (0). The first IP of the host ID is network address. so it would be 0.  Network address is 192.168.0.0/24

Usable IP is the IP address that the host can use on their system.
Ex: 192.168.0.0/24.  24bit is Network ID (192.168.0) and 8bit is Host ID (0). The first IP  is network address and the last IP is broadcase address. The IP address between network address and broadcase address is usable IP address. So the IP ddress from 192.168.0.1 to 192.168.0.254 is usable IP address

Gateways address is the IP address that you can reach and go out from your network to another network.
Ex: 192.168.0.10(You) ———–> 192.168.0.1 (Your router) ————-> 8.8.8.8 (Internet)
So the IP 192.168.0.1 is the address that you can reach and the way you can go to the Internet.

### Calculation

• #### Calculate the number of IP address inside a network

n is the prefix length Or number of network bit. (32 – network bit = host bit)
32 is total bit of IPv4 address

Ex: 192.168.0.0/24
Number Of IP address= 2^(32-24)= 2^8 = 256
The IP range start from 0 to 255. As you know the first IP is network address and the last IP is broadcase address.
Usable IP address= 192.168.0.1 to 192.168.0.254

• #### Calculate prefix length to subnet mask

Example:  /26  it has 26bit, 255.255.255 is equal to 24bit.
26-24=2 so we have 2bit left
2 bit = 192
=> /26 = 255.255.255.192

• #### Summary network

Example: we have 4 network as below:
192.168.0.68/28
192.168.0.129/27
192.168.0.2/30
192.168.0.18/29

1. Look at the number that are the same
=> 192.168.0 are the same

2. There are two way for this step,
– convert the last numer that are not the same to binary
– Do IP range
Now let's see
192.168.0.68/28 has 16 IPs, (16×2=32, 16×3=48, 16×4=64) so range from 192.168.0.64 – 192.168.0.79
192.168.0.125/27 has 32 IPs (32×2=64, 32×3=96, 32×4=128) IP range from 192.168.0.96 – 192.168.0.127
192.168.0.2/30 has 4 IPs (2×2=4) IP range from 192.168.0.0 – 192.168.0.3
192.168.0.18/29 has 8 IPs (8×2=16, 8×3=24) IP range from 192.168.0.16 – 192.168.0.23

What subnet can cover all this ?
– /28 –>  192.168.0.64 – 192.168.0.79
– /27 –>  192.168.0.96 – 192.168.0.127
– /30 –>   192.168.0.0 – 192.168.0.3
– /29 –>   192.168.0.16 – 192.168.0.23

for this 4 prefix we see /27 is the biggest subnet, is it possible to cover other prefix? No.
In this 4 prefix the first range is    /30  –>   192.168.0.0 – 192.168.0.3
and the last range is /27  –>  192.168.0.96 – 192.168.0.127
.
Now we put it togather. The summary IP range should from 192.168.0.0 to 192.168.0.127,  Now let's think which prefix length match for this range, The prefix should be greater then /27

– /26 has 64 IPs the first range start from 0 to 63
– /25 has 128 IPs the first range start from 0 to 127
We see /25 can cover all the above 4 prefix => 192.168.0.0/25 is the summary network

### 2 thoughts on “Simple way for IPV4 subnet calculation”

• January 22, 2019 at 10:45 am

I don't understand x2, x3, x4 ?

• February 15, 2019 at 12:18 am

For example: Prefix length /30 = 255.255.255.252
/30 = 32-30 = 2 host bit
2ˆ2 = 4
So in /30 has 4IP, 1 broadcase, 1 network so usable is 2IP
For example someone give you this IP 192.168.0.16/30 what is IP you can assign to your host or router ?
So the the first range is 0-3, 4-7, 8-11, 12-15, 16-19