# How to: IPv4 Subnettingusing the desired new netmask

Let's say you are given IPv4 address 172.16.68.230, the original subnet mask is 255.255.0.0 and the new subnetmask is 255.255.240.0.
Using these parameters, you need to determine the following:

1. Number of subnet bits
2. Number of subnets that will be created
3. Number of host bits
4. Number of hosts per subnet
2. First host in network of current IP address
3. Last host in network of current IP address

## Solution

To solve this question, we begin by determining how many subnet bits we need to borrow.

The original subnetmask is 255.255.0.0 is written in binary as follows:

```Original subnet mask:       255       . 255       . 0         . 0
Original mask in binary:    1111 1111 . 1111 1111 . 0000 0000 . 0000 0000
```

The new subnetmask is 255.255.240.0 is written in binary as follows:

```Original subnet mask:       255       . 255       . 240       . 0
Original mask in binary:    1111 1111 . 1111 1111 . 1111 0000 . 0000 0000
```

We can now answer questions a and b by counting the number of 1's in each subnetmask.

The number of bits borrowed is 20 - 16 = 4 bits

1. Number of subnet bits:
20 - 16 = 4

We can determine the number of subnets with the formula 2N where N = the number of the subnet bits.

1. Number of subnets created:
24 = 16

Finding the number of host bits is fairly easy. A IPv4 address consists of 32 bits. Our new netmask is 20 bits.

1. Number of host bits:
32 - 20 = 12

So how many hosts can be contained in each subnet? Use the formula 2B - 2 where B = the number of the host bits.
Note that we subtract 2 IP numbers, because the first IP number in the subnet is the network address and the last last IP number is the broadcast address.

1. Number of hosts per subnet:
212 - 2 = 4 094

Halfway there! Now we need to determine the network address of the subnet in which the IP address 172.16.68.230 resides.

To accomplish this, we need to perform the binary AND-function on the IP and its subnet mask:

```IP address in binary:       1010 1100 . 0001 0000 . 0100 0100 . 1110 0110
Subnetmask in binary:       1111 1111 . 1111 1111 . 1111 0000 . 0000 0000
AND _____________________________________________
1010 1100 . 0001 0000 . 0100 0000 . 0000 0000
```

The resulting binary is the network address for this IP and its subnetmask. Now convert the bytes back to decimal:

1010 1100 . 0001 0000 . 0100 0000 . 0000 0000 = 172.16.64.0

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 hhhh . hhhh hhhh
First host in binary:       1010 1100 . 0001 0000 . 0100 0000 . 0000 0001
converted to decimal:       172       . 16        . 64        . 1
```
1. First host in network of current IP address:
1010 1100 . 0001 0000 . 0100 0000 . 0000 0001 = 172.16.64.1

The broadcast address of a IPv4 network is always the last possible number in the address range.
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 hhhh . hhhh hhhh
Broadcast in binary:        1010 1100 . 0001 0000 . 0100 1111 . 1111 1111
converted to decimal:       172       . 16        . 79        . 255
Last host in binary:        1010 1100 . 0001 0000 . 0100 1111 . 1111 1110
converted to decimal:       172       . 16        . 79        . 254
```
1. Last host in network of current IP address:
1010 1100 . 0001 0000 . 0100 1111 . 1111 1110 = 172.16.79.254