Every host on a network has an IP address and a subnet mask.

To find your computer’s IP address or subnet mask:

Linux → ifconfig

Windows → ipconfig/all

MAC → ipconfig -a

Subnet Mask is the same length as IPv4 address — 32-bits. But an IP address and a subnet mask are different. A subnet mask’s binary representation is a series of 1s followed by some number of zeros.

Example:

255.255.255.0 (decimal) = 11111111.11111111.11111111.0 (binary)

The mask has 24 ones followed by 8 zeros, which is represented by ‘/24’.

Why do we need subnets?

Suppose A wants to talk to B which is on the same LAN as A, but A also wants to communicate with C which is somewhere else on the internet. How does A come to know that C is not on its LAN and still hit C with a message?

A compares the address of B with the one of itself. A’s subnet mask can be divided into 2 segments — the ones and the zeros. A compares the IP address of itself with that of B only up to the first segment (up to 1s, called Network ID). A finds out that every bit is the same for itself and B(up to the first segment, i.e. up to 24 bits), and it can infer that B is on the same local area network as A.

NOTE: The part of the subnet mask with all 1s is called Network ID and the latter, 0s part is called Host ID. A divides the IP address of itself and any other device in the same fashion or up to the same length.

Now to check if C is on the same network as A or some other network, A follows the same pattern as before. A compares its IP address with that of C only up to the 1s, and as the result comes different, A can tell that their Network IDs are different and can infer that C is on some other network, or they are remotely connected (not locally).