An IP address consists of two parts: the network ID and the host ID. A network number is used to identify a specific network, while a host number is used to identify a specific host on that network. How these two parts are calculated depends on the class of the IP address (Class A, B, C, D, or E addresses, with classes A, B, C being the most commonly used for regular network communication) and the subnet mask.
An IPv4 address is a 32-bit number, usually represented as four decimal numbers (between 0 and 255 each), separated by dots, such as 192168.1.1。
Determine the IP address category:
Class A address: The first byte is the network number (1-126), and the remaining three bytes are the host number.
Class B address: The first two bytes are the network number (128-191), and the last two bytes are the host number.
Class C address: The first three bytes are the network number (192-223) and the last byte is the host number.
Use Subnet Mask:
The subnet mask is used to further divide the network number and the host number. It is a 32-bit number with all 1 for the network number part and 0 for the host number part.
For example, the default subnet mask for a Class C network is 255255.255.0, the first three bytes are the network number, and the last byte is the host number.
Calculation method: Perform a logical and operation between the IP address and the subnet mask (that is, if both are 1, the result is 1, otherwise it is 0), and the result is the network address.
The host number can be obtained by logically AND arithmetic the IP address and the inverse code of the subnet mask.
Let's say you have an IP address 192168.1.100 and subnet mask 255255.255.0。
Network number calculation:
IP address: 192168.1.100 ->11000000.10101000.00000001.01100100
Subnet Mask: 255255.255.0 ->11111111.11111111.11111111.00000000
After logical and operation: 1100000010101000.00000001.00000000 ->192.168.1.0 (network address).
Host Number Calculation:
Inverse code of subnet mask: 00.0.255 ->00000000.00000000.00000000.11111111
IP address: 192168.1.100 ->11000000.10101000.00000001.01100100
After logical and operation: 0000000000000000.00000000.01100100 ->0.0.0.100 (host number).
Therefore, the network number is 192168.1.0, the host number is 00.0.100。
In practice, especially when dividing subnets, the subnet mask may not be the default value, which affects the calculation of the network number and host number. Understanding the role of subnet masks is critical to correctly calculating the network number and host number.