May 17, 2021

IP address and subnet mask explained

In this document, we're going to talk about an important topic in networking. It's internet protocol. Internet and every other network are built on some rules. We call them network protocols. When developing a network what we expect? The primary goal is to communicate with other devices that are connected to the network. So there should be a way to identify each device. If not, how we can send data to a device?. Let me take the following example.

Lan network

There are some computers in one small network. Now Computer_A wants to send some data to Computer_B. So the sender wants to include the name of the receiver in the data if not this data packet cannot be delivered to the destination. Now when the end device receives the data it also wants to know from where this data is receiving. So the name of the sender should also be included in the data packet.

Now there are a couple of things to notice. First, the name of all devices connected to the network must be unique. If there are a hundred devices, there should be a hundred names. The second thing is this name should be short, simple, and easily usable.

Take the words Computer_A and Computer_B. Both of them are long 10 characters. That means we should allocate 10 bytes (80 bits) for that. If the name is the longer size of that also goes higher. So different names get different lengths and sizes. That is not nice. The solution to this problem is a number with a fixed length. We call this number an IP address. 

If we get this long story short there is a unique number for every device that connected to a network. We call it an IP address. At the moment, there are two IP address schemes. IPV4 and IPV6. The most used version is IPV4. So we are going to talk more about IPV4. In IPV4 we use 32 bits for an IP address. That means 4 bytes. Actually, an IP address looks like this.


But for the easy presentation, we divide it into four parts. Actually, this is how we keep remembering or telling a phone number.


There are 4 parts as the first octet, the second octet, and so on. The above address is In binary format. From our early ages, we have dealt with the decimal number system. Not binary. So it's easy to use an IP address in decimal like the following.

When we think about one byte (8 bits) it can contain 256 different values. So it contains 0 to 255. (00000000 to 11111111). In IPV6 we use 128 bits (16 bytes). An IPV6 address will look like the following.


I hope you got a clear idea about this concept. Now, let us move into the next topics. When we think about a LAN such as in the above image we know all devices that are connected to the network. So we can manually assign an IP address to each device. (Actually, In the above example, I have put a switch as the central device. It can't assign IP addresses. For the moment assume we are assigning IP addresses manually) . So if there are a hundred devices we can give hundred IP addresses. No problem.

But think about the following network. There are three small networks. All of these networks contain a number of computers and those networks are connected through routers.



Let's name the network which connected to the Router_1 as Network_1 And the network connected to the Router_2 as Network_2, and so on. 

So how we assign IP addresses to these devices. Let's assume the network_1 has 100 computers and  network_2 has 200 computers. So we need to give 100 IP s for the network_1 and 200 IP s for the network_2. But how we can ensure this number of devices will remain the same. There may be some cases we add new devices to a network. So we can't give a fixed number of IP addresses.


What we can do is give an IP address range for a network.  What does mean by an IP range? For example, let's consider IP addresses from to It is an IP range. All IP address such as,,, etc is included in this range. The last IP in this range will be Following is the way we represent the above IP range.

Okay. Let's think we have given the IP range to the network_1, IP range to the network_2, and IP range to the network_3.

Now both computer_A and computer_B are in the  Network_1. Let's say computer_A sends a data packet to computer_B. First, the packet will go to the central device switch.  The switch knows all the IP addresses of its connected computers. So it can forward the data packet to the relevant computer. So in this case that data packet will reach Computer_B.

What if the computer_A wants to send a data packet to computer_D ?.   computer_A is in the Network_1 while computer_D is located in the network_2.  Let's say the IP address of the Computer_D is We know when Computer_A sends the data packet it reaches the central switch. The switch sees the destination IP address is  But it does not know any device connected to it with that address. So what it can do is forward that data packet to the router. We call that the default gateway. That is the point a data packet goes out from the local network.

So when the Router_1 receives this data packet how it understands where it should be forwarded? There are two other networks connected to Router_1 other than the above one. What we have to do is give all details about connected networks to the router when we setting it up.

sender to know the name of the receiver and the name of the network that the receiver is located on. Because computer B is not in the same network. This is the time things getting interesting. As a solution to this problem, we divide IP addresses into two parts. One is network ID. It contains the name of the network. The second part is host id. That one contains the name of the device. Let's take an example.


In mathematics, there is a concept called permutations and combinations. By using that(specially permutations) we can calculate how many different numbers can be made with a given set of numbers. So when we think about one byte there are 256 different numbers. In a combined two bytes we can put 65,534 different numbers. If we allocate two bytes for network ID and another two bytes for host ID we can divide it like this.  If you give one byte for network ID and three bytes for host ID the number of networks is limited and there are more hosts that can be a network.  So there are some IP address classes and they use their own ways to divide network ID and host ID. So how we can identify an IP address class? To deliver a packet of data computer must know which part contains network ID and what is the host ID. To do this task we use the first 3 or 4 bits of an IP address. You know that an IP address is something like this. Class A If the first bit is 0, the address is a Class A address. Class B If the first bit is 1 and the second bit is 0, then it is a Class B address. Class C If the first two bits are 1 and the third bit is 0, then it is a Class C address. OK. That's all for this tutorial. In the next post, I'll explain subnets. Thank you for reading. Don't forget to put a comment and share it.

Jul 02
Reverse engineering tutorial for beginners

So you want to learn Reverse engineering. That's great. RE is used in various topics such as....

Jun 13
Build A Simple Web shell

A web shell is a piece of code written to get control over a web server. It is helpful for....

Jul 02
PHP Secure fie uploading

Hear I'm going to share a easy and secure way to upload your images and other files. You have to be....

Replying to 's comment Cancel reply
Thilan Danushka Dissanayaka

Thilan Dissanayaka

Hi, I'm Thilan from Srilanka. An undergraduate Engineering student of University of Ruhuna. I love to explorer things about CS, Hacking, Reverse engineering etc.