# How to: IPv4 Subnettingusing the desired number of networks

Let's say you are given the network range 172.16.68.0/24. Now you're asked to create 7 subnets in this range by subnetting.
Using these parameters, you need to determine the following:

1. The actual number of subnets to create
2. Number of subnet bits to borrow from the hostportion
4. Number of hosts per subnet
1. Networkaddress of the second subnet
2. First host in the second subnet
3. Last host in the second subnet

## Solution

We are asked to create 7 subnets in the available range. Howevery, we can't optimally divide the network range in 7 equal subnets, because the number of possible subnets for any given range is always a power of 2.

Instead, we must determine the next highest power of 2, which is 8. This means we will not be using the last (eighth) subnet.

1. Actual networks to create
next highest power of two = 8

Knowing the optimal number of subnets to create, we should now calculate the new subnet mask each subnet should be using

First we need to determine how many extra bits to use in the new subnet mask. There's a simple rule: each bit added will multiply the previously available number subnets by 2. Using 1 extra bit creates 2 subnets, 2 extra bits creates 4 and 3 extra bits creates 8.

Of course, you can also mathematically determine this using the formula S = 2N where N = the number of the subnet bits and S = the number of subnets.
Thus, changing the formula to get number of bits to borrow makes it N = Log2(S). This results in, you guessed it, 3.

1. Subnet bits to borrow
Log2(8) = 3

Time to write this out in binary so we can calculate the new subnet mask. Notice the three added subnet bits.

```Original mask length:       /24
Original mask in binary:    1111 1111 . 1111 1111 . 1111 1111 . 0000 0000
Original mask in decimal:   255       . 255       . 255       . 0
New mask in binary:         1111 1111 . 1111 1111 . 1111 1111 . 1110 0000
New mask in decimal:        255       . 255       . 255       . 224
```
1111 1111 . 1111 1111 . 1111 1111 . 1110 0000 = 255.255.255.224

To calculate the number of hosts per subnet we need to check how many host bits are left in the subnetmask. A IPv4 address consists of 32 bits. Our new netmask is 27 bits. This leaves 32 - 27 = 5 bits for use as host IPs.

Now we can satisfy the formula 2B - 2 where B = the number of the host bits.

1. Number of host bits:
32 - 27 = 5
2. Number of hosts per subnet:
25 - 2 = 30

Halfway there!

The final four questions are all about calculating some interesting addresses in the second subnet.

Let's keep the overview by writing out every possible network using the new subnetmask:

```Net 1:      1010 1100 . 0001 0000 . 0100 0100 . 0000 0000   =  172. 16. 68.   0/27
Net 2:      1010 1100 . 0001 0000 . 0100 0100 . 0010 0000   =  172. 16. 68.  32/27 (to calculate)
Net 3:      1010 1100 . 0001 0000 . 0100 0100 . 0100 0000   =  172. 16. 68.  64/27
Net 4:      1010 1100 . 0001 0000 . 0100 0100 . 0110 0000   =  172. 16. 68.  96/27
Net 5:      1010 1100 . 0001 0000 . 0100 0100 . 1000 0000   =  172. 16. 68. 128/27
Net 6:      1010 1100 . 0001 0000 . 0100 0100 . 1010 0000   =  172. 16. 68. 160/27
Net 7:      1010 1100 . 0001 0000 . 0100 0100 . 1100 0000   =  172. 16. 68. 192/27
Net 8:      1010 1100 . 0001 0000 . 0100 0100 . 1110 0000   =  172. 16. 68. 224/27 (unused)
```

We now know the network address of the second subnet.

1. Network address of net 2:
1010 1100 . 0001 0000 . 0100 0100 . 0010 0000 = 172.16.68.32

To determine the first valid host in this subnet, we increment the host portion of the network address by 1

```Mask:                       nnnn nnnn . nnnn nnnn . nnnn nnnn . nnnh hhhh
First host in binary:       1010 1100 . 0001 0000 . 0100 0100 . 0010 0001
converted to decimal:       172       . 16        . 68        . 33
```
1. First host in net 2:
1010 1100 . 0001 0000 . 0100 0100 . 0010 0001 = 172.16.68.33

The broadcast address of a IPv4 network is always the last possible number in the network.
We find this number by filling up all the bits of the networkaddress with ones, and then converting the binary values back to decimals.

The last valid host IP is the broadcast number decremented by 1. We can use the same binary method to determine the decimal numbers:

```Mask:                       nnnn nnnn . nnnn nnnn . nnnn nnnn . nnnh hhhh
Broadcast in binary:        1010 1100 . 0001 0000 . 0100 0100 . 0011 1111
converted to decimal:       172       . 16        . 68        . 63
Last host in binary:        1010 1100 . 0001 0000 . 0100 0100 . 0011 1110
converted to decimal:       172       . 16        . 68        . 62
```
1. Last host in net 2:
1010 1100 . 0001 0000 . 0100 0100 . 0011 1110 = 172.16.68.62