🎭 Subnet Mask: The Complete Guide

📌 Definition

A Subnet Mask is a 32-bit number that accompanies an IP address. It has one specific job: To tell the computer which part of the IP address is the "Network" and which part is the "Host".

Without a Subnet Mask, an IP address is just a meaningless string of numbers. The mask gives it context.


⚡ How It Works (The Logic)

The Subnet Mask works like a filter (or a mask) using Binary logic.

Binary Visualization

Let's look at a standard Class C mask: 255.255.255.0

FormatOctet 1Octet 2Octet 3Octet 4
Decimal2552552550
Binary11111111111111111111111100000000
MeaningNetwork 🔒Network 🔒Network 🔒Host 🔓

What this tells the router:
"Any IP address that matches these first 3 octets is on MY LOCAL NETWORK. Anything else? Send it to the Gateway."


💡 Hinglish Analogy (The Painting Mask)

Imagine you are painting a wall, but you don't want to paint over the window frame. What do you do? You put Masking Tape on it.

"Jahan '1' hai wahan tape laga hai, kuch mat karo. Jahan '0' hai wahan tumhara raaj hai."


📦 Default Subnet Masks (Classes)

Before CIDR, we had Classes. These are still the default masks for these IP ranges.

ClassRange Starts WithDefault MaskCIDRFormat
A1 - 126255.0.0.0/8N.H.H.H
B128 - 191255.255.0.0/16N.N.H.H
C192 - 223255.255.255.0/24N.N.N.H

🧮 How to Calculate Subnet Mask (Step-by-Step)

If someone gives you a CIDR (e.g., /26), how do you get the mask (255.255.255.192)?

Step 1: Find the "Interesting Octet"

Where does the CIDR number stop?

Example: /26 -> Falls in the 4th Octet (It's more than 24, less than 32).
So the first 3 octets are full (255.255.255.x).

Step 2: Calculate "On" Bits in that Octet

Subtract the previous boundary from your CIDR.

Step 3: Add the Values

Use the Magic Table:
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |

Result: 255.255.255.192


My Handwritten Notes

1768485944425
1768485955461

⚡ Exam Tips

  1. ANDing: The Router performs a logical AND operation between the IP and the Mask to find the Network ID.
  2. Length: A Subnet Mask is ALWAYS 32 bits long.
  3. Contiguous 1s: A valid subnet mask must have a continuous stream of 1s followed by 0s. You cannot have 11011....