If you said because an IP is compounded by four octets of numbers, that's it, you're a genius, thank you by your reading. No, wait a bit, we're going to dig a bit deeper here.


Exemplifying, being compound of four octet of numbers, means that each octet, can be represented by 8 bits or one byte. Using 8 bits, we have a decimal range from 0 to 255, being able to represent 256 numbers. That's why this is the maximum number possible in each octet (255.255.255.255), better saying, the max number possible is 255 because we still need to consider 0. What's funny is, if we can represent the bits in decimal, how can we represent the decimal in bits?


To answer this question we first need to have an IP, let's get the IP from my WSL (Windows Subsystem for Linux):




At this moment my private IP is 172.27.63.246, this range belongs to Class B, but before translating this IP into binary we need to take a look at the following table, that it's representing how we can convert from decimal to binary.


Decimal 128 64 32 16 8 4 2 1
Bits 8 7 6 5 4 3 2 1


Little parenthesis, will blow your mind. If you sum all the above decimal numbers you will have the maximum number of your network mask, 1+2+4+8+16+32+64+128=255. In computing, everything has a reason to be. Now, backing to the private IP, the first octet in decimal is 172, we are going to translate it to binary. The algorithm is quite simple, you need to start from the last bit on the left (128):

  • In the first interaction, you only need to ask, "Is this number (128 [first element]) greater than this decimal (172)?", if the answer is no, you write 1, else you write 0.

  • In the other interactions, the question changes a bit, for example, in the second (nth) interaction, we can ask "Is the sum of the numbers ([first element] 128 + 64 [current]) greater than this octet (172)?", if the answer is no, you write 1, else you write 0 - another question, same output.

  • Repeat it until the last bit on the right. You will keep summing until having the decimal you want.

You understood nothing, right? I need to get better on explanations. Let's make the first octet together.


Decimal 128 64 32 16 8 4 2 1
172 1 0 1 0 1 1 0 0

Basically, 128+32+8+4=172. Ok, great! We just translated the decimal to binary. Check that the magic for this work is to jump out (write zero instead of one) the bits that the number would result in a bigger number than the decimal (172), like 128+64=192. The full table would be like this:


Decimal 128 64 32 16 8 4 2 1
172 1 0 1 0 1 1 0 0
27 0 0 0 1 1 0 1 1
63 0 0 1 1 1 1 1 1
246 1 1 1 1 0 1 1 0

Which we have:

  • 172 = 128+32+8+4.

  • 27 = 16+8+2+1.

  • 63 = 32+16+8+4+2+1.

  • 246 = 128+64+32+16+4+2.

This way, 172.27.63.246 becomes 10101100.00011011.00111111.11110110.


Did you know that we are running out of IPv4 addresses?

Yes, we do. That's because the IP classes are a bit messy and in the past, they didn't expect that we would need so many IPs addresses. Today almost everyone has multiple devices in their home, and each one of them has an IP attached.


It's important to highlight that when I say run out of IPv4 addresses, I'm talking about the public ones. IPv4 addresses were distributed across 5 classes, each class is different from another in the host portion and the network portion, but this is a talk to another article.


How to waste IPv4 addresses like a pro?

Above you saw that I said that we're wasting a lot of IPs, you may ask yourself how, but I'll show you one of the examples, one of the the worst probably.


If you are a developer you already know that your computer has a loopback interface, that we can use for tests, let's take a look at mine.




Now the best part. Above you can see the address 127.0.0.1, and the mask 255.0.0.0, do you know what it means? This means that only the first octet (127) is fixed, the other three octets can be between anything from 0 and 255 (as you learned). In other words, you have a possible combination of 255x255x255=16.581.375 IPs. Sixteen million IPs, YES! They reserved sixteen million IP for your local tests. I know that you only use one, our preferred, 127.0.0.1, but if you want to use 127.1.5.9 or 127.255.0.31, it will respond the same way as 127.0.0.1.




Conclusion

Understand more about network it's pretty necessary to understand how things work in the computing world. In this article, we saw that IPv4 it's pretty fun but also has some problems. We are running out of IPv4 Addresses, to be precise we only have 232 - why an IPv4 has 32 bits? - or 4.3 billion IP addresses, that's why you need to pay to have one and that's why private IPv4 addresses and NAT Gateway saved us before IPv6.



Kenerry Serain

Cloud and Software Architect. Programming and Computing lover and Certified Kubernetes Administrator (CKA), Certificated Kubernetes Developer (CKAD), Certified Kubernetes Security Specialist (CKS) and Certified AWS Solutions Architect.