This blog post will show how IP addresses work, what subnetting is.
Binary to decimal for two byte numbers
First, it can be beneficial to memorize the following table, which is just a list of binary digits.
128 64 32 16 8 4 2 1
It makes it easy to convert between binary and decimal.
table: 128 64 32 16 8 4 2 1
binary: 1 0 0 1 1 0 0 1
gives (by using logical AND and adding the numbers):
128 + 16 + 8 + 1 = 153 as a decimal, thus:
(binary) 1001 1001 = 153 (decimal)
IP Addresses
As an example, lets look at the IP address: 192.168.1.5. Each number between the '.' is one octet (2 bytes). First, we break it down into binary numbers:
decimal| | binary |
---|---|
192 | 1100 0000 |
168 | 1010 1000 |
1 | 0000 0001 |
5 | 0000 0101 |
So, an IP address can be broken up into 4 octets, which is equal to (4 * 8) 32 bits.
Subnet mask
A subnet mask is used by the TCP/IP protocol to determine whether a host is on the local subnet or on a remote network. It is not possible from the IP address alone to determine whether or not the IP refers to a local or remote network. In order to figure this out, the network mask is needed. The network mask is another 32 bit number. For example, a subnet mask could be 255.25.255.0 (in binary 11111111.11111111.11111111.00000000). By taking the logical AND between the IP address and Subnet Mask you can find the Network address and the Host address.
Network ID and subnet mask
Given a network ID: 192.168.1.0, how do we find the IP behind it?
If we can write the CIDR notation like this: 192.168.1.0/24 (CIDR=Classless Interdomain routing). The '/24' tells us how many binary digits are turned on in the subnet mask. 24 binary digits can be shown like this:
1111 1111 . 1111 1111 . 1111 1111 . 0000 0000
In digital it becomes:
255.255.255.0
This is actually the Subnet Mask.
Given a specific Subnet Mask, 255.255.248.0, how do we find the CIDR notation? From the Subnet Mask, we can see that the first 2 octets (255.255) are both = 255, so we need to figure out how many bits 248 is. We can do that by using our table from above:
table: 128 64 32 16 8 4 2 1
We add together from left to right until we hit 248:
128 + 64 = 192 (still less than 248)
128 + 64 + 32 = 224 (still less than 248)
128 + 64 + 32 + 16 = 240 (still less than 248)
128 + 64 + 32 + 16 + 8 = 248
So we know that there are 5 subnetting bits. The first two octets of our Mask (the 255.255) holds 8 bits each (255 = 1111 1111) that are turned on, we now need to add our 5 subnetting bits, so we get:
8 + 8 + 5 = 21
which represents our CIDR (255.255.248.0/21).
Lets say we have an IP address: 192.168.40.55, how do we find the Network ID? Lets write our IP as binary and do a bit of 'calculations':
IP: 192.168.40.55
Binary: 1100 0000 . 1010 1000 . 0010 1000 . 0011 0111
Subnet: 1111 1111 . 1111 1111 . 1111 1000 . 0000 0000
Do a logical AND
Network ID: 1100 0000 . 1010 1000 . 0010 1000 . 0000 0000
in digital: 192 . 168 . 40 . 0
So our Network ID is 192.168.40.0/21 (CIDR notation).
Subnet mask: 255.255.248.0
This leaves the whole 4th octet and 3 bits of the third octet as hosts bits, which allows for quite a lot of hosts. Calculating the number of available IPs in the hosts bits is done like this:
We have 3 bits of the 3rd octet and the full 4th octet:
Binary: 0111 1111 1111
Decimal: 2047
So 2047 available IP addresses.
Example 1
Mask: 255.255.248.0/21 and IP: 192.168.45.55. How do we find the Network ID?
IP: 192.168.45.55
Binary: 1100 0000 . 1010 1000 . 0010 1101 . 0011 0111
Subnet: 1111 1111 . 1111 1111 . 1111 1000 . 0000 0000
Do a logical AND
Network ID: 1100 0000 . 1010 1000 . 0010 1101 . 0000 0000
in digital: 192 . 168 . 45 . 0
So our Network ID is 192.168.45.0/21 (CIDR notation).
Subnet mask: 255.255.248.0
Example 2
Mask: 255.255.255.192.
a) What is the CIDR notation?
We know that the first 3 octets each have 8 bits turned on, so the issue becomes to find the number of bits turned on in the 4th octet (192).
table: 128 64 32 16 8 4 2 1
We add together from left to right until we hit 192:
128 + 64 = 192
digital: 192
binary: 1100 0000
So our 4th octet has 2 bits turned on (the 128 bit and the 63 bit). Adding all the turned on bits together: 8 + 8 + 8 + 2 = 26, so our CIDR notation becomes 255.255.255.192/26.
b) Given IP address 192.168.45.55, what is the Network ID?
IP: 192.168.45.55
Binary: 1100 0000 . 1010 1000 . 0010 1101 . 0011 0111
Subnet: 1111 1111 . 1111 1111 . 1111 1111 . 1100 0000
Do a logical AND
Network ID: 1100 0000 . 1010 1000 . 0010 1101 . 0000 0000
in digital: 192 . 168 . 45 . 0
So our Network ID is 192.168.45.0/26 (CIDR notation).
Subnet mask: 255.255.248.192
Example 3
a) Determine the address range covered by the network: 192.168.4.0/22?
Lets first find the subnet mask from the '/22'. The first 2 octets in the subnet masks will be 255 (adding up to the first 16 bits), this leaves (22-16) 6 bits left which (binary: 1111 1100 = 252) equals 252, so the subnet mask becomes 255.255.252.0.
In binary, the network ID is the first 22 bits of the IP in binary:
(192.168.4.0/22) '1100 0000.1010 1000.0000 01|00.0000 0000' = 1100 0000.1010 1000.0000 01
The available addresses will be:
from: 1100 0000 . 1010 1000 . 0000 0100 . 0000 0000
to: 1100 0000 . 1010 1000 . 0000 0111 . 1111 1111
and in decimal:
from: 192.168.4.0
to: 192.168.7.255