# Logical AND with subnet mask

### Logical AND with subnet mask

A subnet mask is used to indicate the dividing position between the network address part and the host address part on the IP address.

In the subnet mask, the partition position between the network address part and the host address part is indicated by expressing the network address part as "1" and the host address part as "0".

In addition to indicating the partition position between the network address part and the host address part, the subnet mask can be used to know the network address to which the IP address belongs by taking the AND with the IP address.

First of all, we will explain using the subnet mask in 8-bit increments, which is easy to calculate.

First, I will explain using "150.10.1.1/16".

You can see that '150.10.1.1/16' is class B and uses the default subnet mask '255.255.0.0'.

Obtain the logical product (AND operation) of "150.10.1.1/16" and "255.255.0.0".

What is logical AND?

0 AND 0 = 0
0 AND 1 = 0
1 AND 0 = 0
1 AND 1 = 1

It is an operation that becomes It is summarized in the table below.

To perform an AND operation with "150.10.1.1/16" and "255.255.0.0", it is easier to understand if converted to binary numbers, so convert to binary numbers. *In the case of subnets in 8-bit increments, it can be calculated immediately with mental arithmetic.

"150.10.0.0" obtained by the AND operation becomes the network address to which "150.10.1.1" belongs.

Next, I will explain using "150.10.1.1/24". It's basically the same as the previous method.

To perform an AND operation with "150.10.1.1/24" and "255.255.255.0", it is easier to understand if converted to binary numbers, so convert to binary numbers. *In the case of subnets in 8-bit increments, it can be calculated immediately with mental arithmetic.

"150.10.1.0" obtained by the AND operation becomes the network address to which "150.10.1.1" belongs.

In this way, you can use the subnet mask to determine the network address to which the IP address belongs.

Calculations are easy if the subnet mask is in 8-bit increments.

that is,

(255) 10  = (11111111) 2

This is because the AND operation with "255" always becomes the number itself.

Also, an AND operation with "0" will always result in "0".

In other words, if the subnet mask is in 8-bit increments, the network to which it belongs can be known just by looking at it, without the need to convert it to binary numbers and perform calculations.

In the above example, we explained using subnet masks in 8-bit increments, which are easy to calculate. Here, we will explain using a subnet mask that is not in 8-bit increments.

### Example 1

First, I will explain using "192.168.1.33/28".

You can see that "192.168.1.33/28" is class C and uses "255.255.255.240" as the subnet mask.

Calculate the logical product (AND operation) of "192.168.1.33/28" and "255.255.255.240".

To perform the AND operation with "192.168.1.33/28" and "255.255.255.240", it is easier to understand if converted to binary, so convert to binary.

"192.168.1.32" obtained by the AND operation becomes the network address to which "192.168.1.33" belongs.

"192.168.1.32/28" is the second subnet of the "192.168.1.0/24" network.

In other words, "192.168.1.33/28" belongs to the second subnet of the "192.168.1.0/24" network.

### Example 2

Next, I will explain using "192.168.1.33/26".

You can see that "192.168.1.33/26" is class C and uses "255.255.255.192" as the subnet mask.

Obtain the logical product (AND operation) of "192.168.1.33/26" and "255.255.255.192".

To perform an AND operation with "192.168.1.33/26" and "255.255.255.192", it is easier to understand if converted to binary, so convert to binary.

"192.168.1.0" obtained by the AND operation becomes the network address to which "192.168.1.33" belongs.

"192.168.1.32/26" is the 0th subnet of the "192.168.1.0/24" network.

In other words, "192.168.1.33/26" belongs to the 0th subnet of the "192.168.1.0/24" network.

Here, you will notice that the networks (subnets) to which ``192.168.1.33/28'' and ``192.168.1.33/26'' belong are different, even though they have the same IP address ``192.168.1.33''.

In other words, when notating an IP address, it is not possible to know the network to which the IP address belongs if only notating the IP address. By adding the subnet length with "/" to the right of the IP address, you can know the network to which the IP address belongs.

I explained that you can use an IP address and a subnet mask to find out which network address the IP address belongs to. This work is done when computers communicate.

### Benefits of subnetting

Subnetting can be done by using a subnet mask. By creating subnets that match the scale of the network, it is possible to operate the network efficiently.

The network in the diagram below illustrates the benefits.

The default subnet mask for each class of IP address is as follows.

• Class A ・・・ 255.0.0.0
• Class B ・・・ 255.255.0.0
• Class C ・・・ 255.255.255.0

So, by default, the IP addresses of hosts for each class that can be used are as follows:

Class A ・・・ 2 24  - 2 = 16,777,214

Class B ・・・ 2 16  -2 = 65,534

Class C ・・・2 8  - 2 = 254

The diagram above uses Class B IP addresses. With the default subnet mask, you end up with a large network of 65,534 devices. I don't think there are many such networks in the world, and if there are so many terminals in one segment, congestion will occur.

In the network above, class B IP addresses are used as if they were class C IP addresses by extending 8 bits from "/16" to "/24".