# What is subnetting?

Subnetting plays a big role in IP networking these days. Network admins rely on it to divide large networks into smaller, more manageable pieces. The goal is to use IP addresses in a smarter way overall. It reduces broadcast traffic quite a bit and simplifies routing tasks too. You do this by tweaking the bits within an IP address. That way, the network part gets separated from the host part in a clear manner.

<mark style="color:blue;">**IP Address Classes**</mark>

IPv4 addresses are divided into different classes. This configuration allows networks to be sorted according to their size and intended use. Each class defines exactly which section of the IP address covers the network itself. It also defines which section covers individual hosts.

There are various types of classes that differ in terms of how many bits are allocated per host and how many per network, as well as the subnet mask:

<table><thead><tr><th width="76">Class</th><th width="160">First Octet Range</th><th width="184">Default Subnet Mask</th><th width="138">Network Bits</th><th>Host Bits</th><th>Max Hosts</th></tr></thead><tbody><tr><td>A</td><td>1–127</td><td>255.0.0.0 (/8)</td><td>8</td><td>24</td><td>16,777,214</td></tr><tr><td>B</td><td>128–191</td><td>255.255.0.0 (/16)</td><td>16</td><td>16</td><td>65,534</td></tr><tr><td>C</td><td>192–223</td><td>255.255.255.0 (/24)</td><td>24</td><td>8</td><td>254</td></tr><tr><td>D</td><td>224–239</td><td>N/A</td><td>Multicast</td><td>N/A</td><td>N/A</td></tr><tr><td>E</td><td>240–255</td><td>N/A</td><td>Experimental</td><td>N/A</td><td>N/A</td></tr></tbody></table>

This table allows you to know immediately if an IP like 180.40.0.0 is /16 (Class B) or 200.100.50.0 is /24 (Class C) without memorizing ranges.

<figure><img src="https://2833470610-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxEQmpwmdm3I8FZ925fn8%2Fuploads%2FjfyPifPiOHI1VzOHMW7Y%2Fimage.png?alt=media&#x26;token=42defd64-44c7-4656-a971-6d87469d45e7" alt=""><figcaption></figcaption></figure>

<mark style="color:blue;">**Why Subnetting Matters**</mark>

Devices in any network always need their own unique IP addresses. Still, a big network without subnetting tends to waste addresses and create way too much broadcast traffic. When you divide the network into segments, each subnet gets its own set of IP addresses. That setup makes everything more organized and easier to expand over time. Subnetting helps with security rules too. It also improves routing choices by keeping traffic separate across different subnets.

<mark style="color:blue;">**Bitwise Calculations**</mark>

Bitwise calculations sit right at the center of how subnetting works. IP addresses in IPv4 come as 32-bit numbers. Subnet masks decide which bits belong to the network portion and which go to the hosts. You rely on operations like **AND** and **OR** to figure out the main addresses.

* The **Network ID** results from a bitwise AND of the IP address and the subnet mask. It points directly to the subnet and stays unassigned to any host.
* The **Broadcast Address** comes from ORing the IP address with the inverted subnet mask. That one sends messages out to every device in the subnet.
* **Host addresses** fill all the spots between the network ID and the broadcast address. Those are the ones you can assign to actual devices.

Understanding bitwise operations is very important for a solid understanding of subnets. It gives you the tools to accurately identify the exact address ranges in each subnet.

<mark style="color:blue;">**FLSM vs VLSM**</mark>

Subnetting can be done in two main ways:

* **FLSM** stands for **Fixed Length Subnet Mask**. Every subnet in this setup uses the same number of host bits. That makes things simpler and more predictable overall. Still, it can waste addresses if different subnets need varying sizes. For instance, when you divide a `/24` network into eight subnets, each one gets exactly 32 addresses. This happens no matter what the actual host needs turn out to be.
* **VLSM** means **Variable Length Subnet Mask**. Here, subnets can use different numbers of host bits based on their specific requirements. This approach really maximizes address efficiency in the end. But it does call for more careful planning from the start. With VLSM, you can assign larger subnets to areas that have more hosts. You can also set up smaller ones in spots where fewer addresses are needed.

<mark style="color:blue;">**Special Cases: /31 and /32**</mark>

Some subnet masks are rarely used but important in specific scenarios:

* **/31 Subnet Mask:** It holds only two IP addresses in total. People use it for point-to-point links, such as connections between routers. In these cases, both addresses work fine. There is no need for a separate network or broadcast address when you have just two hosts.
* **/32 Subnet Mask:** Contains a single IP address. It shows up often for loopback interfaces. Folks also use it to specify one host in routing policies. You see it in firewall rules too. It helps in VPN configurations to point to exactly one device. These special cases play a key role in advanced networking.

<mark style="color:blue;">**Example: Subnetting a Class C Network with Binary Representation**</mark>

Let's consider a Class C network: `200.100.50.0/24`. Suppose we want to divide it into **8 subnets**.

**Step 1: Convert the IP to binary**

```
200.100.50.0 → 11001000.01100100.00110010.00000000
```

**Step 2: Determine the number of subnet bits**

To create 8 subnets, we need **3 bits** (because 2^3 = 8). In a Class C network, the last octet originally contains 8 bits for hosts. We will take 3 bits from these for subnetting, leaving 5 bits for host addresses.

```
Original IP in binary:  11001000.01100100.00110010.00000000
Subnetting:                                        000|00000
- Red bits: fixed network portion
- Orange bits: subnet identifier
- Blue bits: host identifier
```

**Step 3: Calculate subnets**

Each subnet will have `2^5 - 2 = 30` usable host addresses. The `-2` accounts for the network ID and broadcast address.

<table><thead><tr><th width="95">Subnet</th><th width="148">Network IP (dec)</th><th width="359">Network IP (bin)</th><th width="148">Broadcast (dec)</th><th width="315">Broadcast (bin)</th><th width="196">Usable Hosts (dec)</th></tr></thead><tbody><tr><td>1</td><td>200.100.50.0</td><td><mark style="color:red;">11001000.01100100.00110010</mark>.<mark style="color:orange;">000</mark>|<mark style="color:blue;">00000</mark></td><td>200.100.50.31</td><td><mark style="color:red;">11001000.01100100.00110010</mark>.<mark style="color:orange;">000</mark>|<mark style="color:blue;">11111</mark></td><td>200.100.50.1 - 200.100.50.30</td></tr><tr><td>2</td><td>200.100.50.32</td><td><mark style="color:red;">11001000.01100100.00110010</mark>.<mark style="color:orange;">001</mark>|<mark style="color:blue;">00000</mark></td><td>200.100.50.63</td><td><mark style="color:red;">11001000.01100100.00110010</mark>.<mark style="color:orange;">001</mark>|<mark style="color:blue;">11111</mark></td><td>200.100.50.33 - 200.100.50.62</td></tr><tr><td>3</td><td>200.100.50.64</td><td><mark style="color:red;">11001000.01100100.00110010</mark>.<mark style="color:orange;">010</mark>|<mark style="color:blue;">00000</mark></td><td>200.100.50.95</td><td><mark style="color:red;">11001000.01100100.00110010</mark>.<mark style="color:orange;">010</mark>|<mark style="color:blue;">11111</mark></td><td>200.100.50.65 - 200.100.50.94</td></tr><tr><td>4</td><td>200.100.50.96</td><td><mark style="color:red;">11001000.01100100.00110010</mark>.<mark style="color:orange;">011</mark>|<mark style="color:blue;">00000</mark></td><td>200.100.50.127</td><td><mark style="color:red;">11001000.01100100.00110010</mark>.<mark style="color:orange;">011</mark>|<mark style="color:blue;">11111</mark></td><td>200.100.50.97 - 200.100.50.126</td></tr><tr><td>5</td><td>200.100.50.128</td><td><mark style="color:red;">11001000.01100100.00110010</mark>.<mark style="color:orange;">100</mark>|<mark style="color:blue;">00000</mark></td><td>200.100.50.159</td><td><mark style="color:red;">11001000.01100100.00110010</mark>.<mark style="color:orange;">100</mark>|<mark style="color:blue;">11111</mark></td><td>200.100.50.129 - 200.100.50.158</td></tr><tr><td>6</td><td>200.100.50.160</td><td><mark style="color:red;">11001000.01100100.00110010</mark>.<mark style="color:orange;">101</mark>|<mark style="color:blue;">00000</mark></td><td>200.100.50.191</td><td><mark style="color:red;">11001000.01100100.00110010</mark>.<mark style="color:orange;">101</mark>|<mark style="color:blue;">11111</mark></td><td>200.100.50.161 - 200.100.50.190</td></tr><tr><td>7</td><td>200.100.50.192</td><td><mark style="color:red;">11001000.01100100.00110010</mark>.<mark style="color:orange;">110</mark>|<mark style="color:blue;">00000</mark></td><td>200.100.50.223</td><td><mark style="color:red;">11001000.01100100.00110010</mark>.<mark style="color:orange;">110</mark>|<mark style="color:blue;">11111</mark></td><td>200.100.50.193 - 200.100.50.222</td></tr><tr><td>8</td><td>200.100.50.224</td><td><mark style="color:red;">11001000.01100100.00110010</mark>.<mark style="color:orange;">111</mark>|<mark style="color:blue;">00000</mark></td><td>200.100.50.255</td><td><mark style="color:red;">11001000.01100100.00110010</mark>.<mark style="color:orange;">111</mark>|<mark style="color:blue;">11111</mark></td><td>200.100.50.225 - 200.100.50.254</td></tr></tbody></table>

**Step 4: Assigning the Gateway**

Typically, the **gateway** is the **first usable host** in each subnet. For example:

* Subnet 1 gateway: 200.100.50.1
* Subnet 2 gateway: 200.100.50.33
* And so on.

**Note**: In some enterprise networks, the gateway can also be assigned to the last usable host of the subnet, depending on the network design and conventions.

<mark style="color:blue;">**Example: Subnetting a Class B Network with Binary Representation**</mark>

Let's consider a Class B network: `180.40.0.0/16`. Suppose we want to divide it into **8 subnets**.

**Step 1: Convert the IP to binary**

```
180.40.0.0/ → 10110100.00101000.00000000.00000000
```

**Step 2: Determine the number of subnet bits**

To create 8 subnets, we need **3 bits** (because 2^3 = 8). In a Class B network, the last two octet originally contains 8 bits for hosts. We will take 3 bits from these for subnetting, leaving 13 bits for host addresses.

```
Original IP in binary:  10110100.00101000.00000000.00000000
Subnetting:                               .000|00000.00000000
- Red bits: fixed network portion
- Orange bits: subnet identifier
- Blue bits: host identifier
```

**Step 3: Calculate subnets**

Each subnet will have `2^13 - 2 = 8190` usable host addresses. The `-2` accounts for the network ID and broadcast address.

<table><thead><tr><th width="95">Subnet</th><th width="148">Network IP (dec)</th><th width="359">Network IP (bin)</th><th width="148">Broadcast (dec)</th><th width="315">Broadcast (bin)</th><th width="196">Usable Hosts (dec)</th></tr></thead><tbody><tr><td>1</td><td>180.40.0.0</td><td><mark style="color:red;">10110100.00101000.000</mark>|<mark style="color:blue;">00000.00000000</mark></td><td>180.40.31.255</td><td><mark style="color:red;">10110100.00101000.000</mark>|<mark style="color:blue;">11111.11111111</mark></td><td>180.40.0.1 - 180.40.31.254</td></tr><tr><td>2</td><td>180.40.32.0</td><td><mark style="color:red;">10110100.00101000.001</mark>|<mark style="color:blue;">00000.00000000</mark></td><td>180.40.63.255</td><td><mark style="color:red;">10110100.00101000.001</mark>|<mark style="color:blue;">11111.11111111</mark></td><td>180.40.32.1 - 180.40.63.254</td></tr><tr><td>3</td><td>180.40.64.0</td><td><mark style="color:red;">10110100.00101000.010</mark>|<mark style="color:blue;">00000.00000000</mark></td><td>180.40.95.255</td><td><mark style="color:red;">10110100.00101000.010</mark>|<mark style="color:blue;">11111.11111111</mark></td><td>180.40.64.1 - 180.40.95.254</td></tr><tr><td>4</td><td>180.40.96.0</td><td><mark style="color:red;">10110100.00101000.011</mark>|<mark style="color:blue;">00000.00000000</mark></td><td>180.40.127.255</td><td><mark style="color:red;">10110100.00101000.011</mark>|<mark style="color:blue;">11111.11111111</mark></td><td>180.40.96.1- 180.40.127.254</td></tr><tr><td>5</td><td>180.40.128.0</td><td><mark style="color:red;">10110100.00101000.100</mark>|<mark style="color:blue;">00000.00000000</mark></td><td>180.40.159.255</td><td><mark style="color:red;">10110100.00101000.100</mark>|<mark style="color:blue;">11111.11111111</mark></td><td>180.40.128.1- 180.40.159.254</td></tr><tr><td>6</td><td>180.40.160.0</td><td><mark style="color:red;">10110100.00101000.101</mark>|<mark style="color:blue;">00000.00000000</mark></td><td>180.40.191.255</td><td><mark style="color:red;">10110100.00101000.101</mark>|<mark style="color:blue;">11111.11111111</mark></td><td>180.40.160.1- 180.40.191.254</td></tr><tr><td>7</td><td>180.40.192.0</td><td><mark style="color:red;">10110100.00101000.110</mark>|<mark style="color:blue;">00000.00000000</mark></td><td>180.40.223.255</td><td><mark style="color:red;">10110100.00101000.110</mark>|<mark style="color:blue;">11111.11111111</mark></td><td>180.40.192.1- 180.40.223.254</td></tr><tr><td>8</td><td>180.40.224.0</td><td><mark style="color:red;">10110100.00101000.111</mark>|<mark style="color:blue;">00000.00000000</mark></td><td>180.40.255.255</td><td><mark style="color:red;">10110100.00101000.111</mark>|<mark style="color:blue;">11111.11111111</mark></td><td>180.40.224.1- 180.40.255.254</td></tr></tbody></table>

**Step 4: Assigning the Gateway**

As mentioned previously, the gateway can be either the **first usable IP** or the **last usable IP** in each subnet, depending on network design preferences.

<mark style="color:blue;">**Example: VLSM Subnetting with Binary Representation**</mark>

Suppose we have a network `192.168.1.0/24` and we need three subnets with 50, 20, and 10 hosts.

**Step 1: Convert the IP to binary**

```
192.168.1.0 → 11000000.10101000.00000001.00000000
```

**Step 2: Determine subnet bits for each subnet**

* **Subnet 1 (50 hosts)**: Needs 6 host bits (2^6 - 2 = 62 usable addresses)\
  New subnet mask: /26 → 255.255.255.192\
  Binary representation:

  ```
  Original: 11000000.10101000.00000001.00000000
  Subnetting:                          00|000000
  ```
* **Subnet 2 (20 hosts)**: Needs 5 host bits (2^5 - 2 = 30 usable addresses)\
  New subnet mask: /27 → 255.255.255.224\
  Binary representation:

  ```
  Next available: 11000000.10101000.00000001.01000000
  Subnetting:                                001|00000
  ```
* **Subnet 3 (10 hosts)**: Needs 4 host bits (2^4 - 2 = 14 usable addresses)\
  New subnet mask: /28 → 255.255.255.240\
  Binary representation:

  ```
  Next available: 11000000.10101000.00000001.01100000
  Subnetting:                                0011|0000
  ```

**Step 3: Calculate subnet ranges**

<table><thead><tr><th width="96">Subnet</th><th width="161">Network IP (dec)</th><th width="353">Network IP (bin)</th><th width="142">Broadcast (dec)</th><th width="345">Broadcast (bin)</th><th width="231">Usable Hosts</th></tr></thead><tbody><tr><td>1</td><td>192.168.1.0/26</td><td><mark style="color:red;">11000000.10101000.00000001</mark>.<mark style="color:orange;">00</mark>|<mark style="color:blue;">000000</mark></td><td>192.168.1.63</td><td><mark style="color:red;">11000000.10101000.00000001</mark>.<mark style="color:orange;">00</mark>|<mark style="color:blue;">111111</mark></td><td>192.168.1.1 - 192.168.1.62</td></tr><tr><td>2</td><td>192.168.1.64/27</td><td><mark style="color:red;">11000000.10101000.00000001</mark>.<mark style="color:orange;">010</mark>|<mark style="color:blue;">00000</mark></td><td>192.168.1.95</td><td><mark style="color:red;">11000000.10101000.00000001</mark>.<mark style="color:orange;">010</mark>|<mark style="color:blue;">011111</mark></td><td>192.168.1.65 - 192.168.1.94</td></tr><tr><td>3</td><td>192.168.1.96/28</td><td><mark style="color:red;">11000000.10101000.00000001</mark>.<mark style="color:orange;">0110</mark>|<mark style="color:blue;">0000</mark></td><td>192.168.1.111</td><td><mark style="color:red;">11000000.10101000.00000001</mark>.<mark style="color:orange;">0110</mark>|<mark style="color:blue;">1111</mark></td><td>192.168.1.97 - 192.168.1.110</td></tr></tbody></table>

**Step 4: Assigning the Gateway**

As mentioned previously, the gateway can be either the **first usable IP** or the **last usable IP** in each subnet, depending on network design preferences.
