How to calculate IP address and CIDR

Source: Internet
Author: User

How to calculate IP address and CIDR

First, the concept of IP address

An IP address is a 32-bit binary number that consists of a network ID and a host ID two part that is used to uniquely identify a computer on the network. The network ID is used to identify the network segment where the computer is located, and the host ID is used to identify the location of the computer in the network segment. IP addresses are typically expressed in 4 groups of 3-bit decimal numbers, with "." In the middle. Separated. For example, 192.168.0.1.

Supplement (IPV6): The previous 32-bit IP address is called IPV4, with the development of information technology, IPV4 the number of available IP addresses can not meet the daily needs of people, according to the authority predicts that by 2010 to fully apply information technology, everyone needs at least 10 IP addresses, such as: computer, Laptops, mobile phones, and smart refrigerators. In order to solve this problem, the IPV6 specification was developed, and the IPV6 uses 128 bits to represent the IP address, which is represented as 8 sets of 4-bit 16 binary numbers, and the middle is ":" Delimited. For example, Ab32:33ea:89dc:cc47:abcd:ef12:abcd:ef12.

Second, IP address classification

In order to facilitate IP addressing to divide IP addresses into categories A, B, C, D, and E, each IP address specifies the number of bits used in each IP address to represent the network ID and host ID. When the number of bits of the host ID is determined, the number of computers in a network that is more than can be determined, the user can choose a kind of IP address to construct the network structure according to the enterprise's need flexibly.

Class A

Class A addresses the network ID with the first 8 bits of the IP address, and 24 bits after the IP address to indicate the host ID. The Class A address is used to indicate that the first bit of the network ID must start with 0, the other 7 bits can be any value, when the other 7 bits are all 0 is the network ID is the smallest, that is 0, when the other 7 bits are all 1 o'clock Network ID maximum, that is, 127. The network ID cannot be 0, it has a special purpose to represent all network segments, so the network ID is minimum 1, and the network ID cannot be used as a network loop test for 127;127. So the Class A network ID valid range is 1-126 a total of 126 networks, each network can contain 224-2 hosts.

Class B

The class B address represents the network ID with the first 16 bits of the IP address, and 16 bits after the IP address for the host ID. Class B addresses are used to indicate that the first two bits of the network ID must start with 10, the other 14 bits can be any value, when the other 14 bits are all 0 is the network ID is the smallest, that is 128; When the other 14 bits are all 1 o'clock the network ID is the largest and the first byte is the largest, which is 191. Class B IP Address the first byte has a valid range of 128-191, a total of 16,384 class B networks, each Class B network can contain 216-2 hosts (65534 hosts).

Class C

The C-class address represents the network ID with the first 24 bits of the IP address, and the last 8 bits of the IP address represents the host ID. Class C addresses are used to indicate that the first three bits of the network ID must start at 110, the other 22 bits can be any value, when the other 22 bits are all 0 is the network ID is the smallest, the first byte of the IP address is 192, and when the other 22 bits are all 1 o'clock the network ID is the largest and the first byte is the largest, which Class C IP Address the first byte has a valid range of 192-223, a total of 2,097,152 Class C network, each Class C network can contain 28-2 hosts (254 hosts).

Class D

Class D addresses are used for multicast, there is no network ID and host ID, the first byte of Class D IP address must start at 1110, and the other 28 bits can be any value, the valid range for Class D IP address is 224.0.0.0 to 239.255.255.255.

Class E

Class E address reserved experiment, no network ID and host ID, the first byte of Class E IP address four must start with 1111, the other 28 bits can be any value, the class E IP address valid range is 240.0.0.0 to 255.255.255.254. Where 255.255.255.2555 represents the broadcast address.

In practice, only Class A, B, and C three IP addresses can be assigned directly to the host, and Class D and E cannot be assigned directly to the computer.

Third, network ID, host ID and subnet mask

The network ID is used to indicate which network the computer belongs to, the same network ID computer does not need to connect through the router to communicate directly, we have the same network ID of the computer as a network called the Local Network (network segment), the network ID is not the same between the computer communication must be connected through the router, We refer to computers with different network IDs as remote computers.

When an IP address is assigned to a computer, which part of the computer's IP address represents the network ID, which part represents the host ID, is not determined by the class to which the IP address belongs, but is determined by the subnet mask. Subnet determines which subnet an IP address belongs to.

The format of the subnet mask is expressed in consecutive 255 followed by a continuous 0, where 255 of this section represents the network ID, and 0 consecutive parts represent the host ID. For example, the subnet mask 255.255.0.0 and 255.255.255.0.

According to the format of the subnet mask can be found, the subnet mask has 0.0.0.0, 255.0.0.0, 255.255.0.0, 255.255.255.0 and 255.255.255.255 a total of five. Subnet masks in this format have a difference of at least 256 times times the number of hosts in each network, which is not conducive to flexibly assigning IP addresses to the needs of the enterprise. For example, an enterprise has 2000 computers, and the user either assigns them a subnet mask of 255.255.0.0, then the network can contain 65534 computers, which will result in a waste of 63,534 IP addresses; Either the user assigns it 8 255.255.255.0 network, then must use the router to connect this 8 network, causes the network management and the maintenance burden.

The network ID is the IP address and the subnet mask with the operation obtained, the IP address will represent the host ID of the portion of all become 0, indicating that the network ID of the part remains unchanged, the network ID format and IP address is the same as the 32-bit binary number, the host ID is the part that represents the host ID.

Example 1:IP address: 192.168.23.35 Subnet Mask: 255.255.0.0

Network id:192.168.0.0 host id:23.35

Example 2:IP address: 192.168.23.35 Subnet Mask: 255.255.255.0

Network id:192.168.23.0 host Id:35

Iv. Subnets and CIDR

By converting a regular subnet mask to binary, the subnet mask is found to be contiguous binary 1 followed by successive 0, where the subnet mask is 1 in the partial representation of the network ID, and the subnet mask is 0 for the host ID. For example, 255.255.0.0 is converted to binary 11111111 11111111 00000000 00000000.

In the previous example, why not use a continuous 1-part representation of the network ID, and a continuous 0-part representation of the host ID? The answer is yes, the IP addressing technology using this scheme is called non-class inter-domain routing (CIDR). CIDR Technology uses a contiguous 1 portion of the subnet mask to represent the network ID, and a contiguous 0 portion represents the host ID. For example, the network contains 2000 computers, only need to use 11 bits to represent the host ID, with a 21-bit table network ID, the subnet mask is expressed as 11111111.11111111.11100000.00000000, and the conversion to decimal is 255.255.224.0. At this point, the network will contain 2046 computers, neither resulting in a waste of IP addresses or using routers to connect to the network, adding additional administrative maintenance.

CIDR notation: The number of bits of the IP address/network ID, such as 192.168.23.35/21, which represents the network ID with 21 bits.

Example 1:192.168.23.35/21

Subnet mask: 11111111 11111111 11111000 00000000 is 255.255.248.0

Network id:192.168.00010111.0 (where the third byte of the red part indicates the network ID, the other represents the host ID, the network ID is the network ID part remains unchanged the host ID all becomes 0) then the network ID is 192.168.16.0

Starting IP Address: 192.168.16.1 (the host ID cannot be all 0, all 0 means the last digit of the network ID is 1)

End IP Address: 192.168.00010111.11111110 (host ID cannot be all 1, all 1 means local broadcast) then the end IP address is: 192.168.23.254.

Example 2: Divides 163.135.0.0 into 16 subnets, computes the network ID, subnet mask, and start and end IP addresses of the first two subnets.

1th Step: 163.135.0.0/20 is represented in CIDR, then the subnet mask is 255.255.240 (11110000). 0.

2nd step: First network ID (subnet mask and IP address and operation): 163.135.0.0

First IP address: 163.135.0.1 End IP Address: 163.135.15.254;

3rd Step: Second network id:163.135.16.0

First IP address: 163.135.16.1 End IP Address: 163.135.31.254.

A fast calculation method of subnet mask and network ID

The subnet mask for CIDR is a contiguous 1 followed by a 0 representation of the connection, and the subnet mask has the following representation methods:

0000 0000 0

1000 0000 128

1100 0000 128+64=192

1110 0000 128+64+32=224

1111 0000 255-15=240

1111 1000 255-7=248

1111 1100 255-3=252

1111 1110 255-1=254

1111 1111 255

Everyone knows that the decimal number of 11111111 is 255, so how do we calculate the subnet mask quickly? binary 1=1,11=3,111=7,1111=15; so 1111 1110=255-1,1111 1100=255-3,1111 1000=255-8,1111 0000=255-15 is that so soon? As soon as we determine how many bits in the subnet mask represent the network ID, we can write the subnet mask right away. So how do we calculate for 1000 0000,1100 0000 and 1110 0000? 27=8 is 1000 0000=128,1100 0000=128+64,1110 0000=128+64+32, so we do not need to remember how much each, only need to do a simple addition and subtraction to take care of the subnet mask calculation.

The result of the network ID Everyone knows that the network ID part is unchanged, the host ID portion becomes 0, then in the calculation of the network ID, first look at how many bits in the subnet mask to represent the network, corresponding to the IP address conversion to binary only the previous few, such as 192.168.176.15/ 19, the network ID altogether 19 bits, then the network ID the first two bytes is 192.168.x.0 to change the third byte. So how do you quickly calculate the value of the X for this change? We know that the third byte is only three bits for the network ID, the conversion is 176>128, the 1th bit is 1,176-128=48<64, the 2nd bit is 0,48>32 3rd bit is 1, the rest of the calculation is meaningless, all to convert to 0, the network ID is 10100000, then the network The ID is 192.168.160.0, so the likelihood of error is very small.

VI. local and remote network concepts

A computer with the same network ID is called a local network, and a computer in the local network does not need a router connection, and a computer with a different network ID is called a remote network, and the computers in the remote network must communicate with each other through a router.

Example 1:192.168.10.14/28,192.168.10.15/28,192.168.10.16/28,192.168.10.31/28 which are legitimate IP and which are illegal IP addresses?

The host ID is all 0 and the host ID is all 1 illegal IP addresses: 192.168.10.15/28, 192.158.10.16/28, 192.168.10.31/28 are illegal IP addresses.

Example 2:192.168.10.14/28,192.168.10.15/28,192.168.10.16/28 which is not the same network segment?

If the network ID is the same as the same network segment, then 192.168.10.16/28 does not belong to the same network segment.

Calculation method of seven, subnet number and host number

Example: 172.168.34.56/20, divided into how many subnets, each subnet can contain how many hosts.

172.168.34.56 is a class B address, the Class B address with 16 bits for the network ID, the topic 20 bits for the network ID, then the number of subnets is 4 bits, then the subnet has 24 times (that is, 0000, 0001 to 1111 changes).

Because the IP address is 32 bits and the network ID is represented by 20 bits, the number of bits for the host ID is 12 bits, and each subnet can contain 212-2 IP addresses, which can contain 4,096 IP addresses.

Note: Why do you want to reduce the number of subnets when calculating the IP address by 2 minus 2? The reason for the IP address minus 2 is that the host ID cannot be all 0 or all 1, and the subnet does not have this problem.

Viii. public IP and private IP addresses

IP addresses are managed and assigned by the IANA (Internet address assignment Authority), and any one IP address that is to be used on the Internet must be assigned by the IANA, and the IANA-assigned IP addresses that are normally used on the internet are called public IP addresses The IANA retains a portion of the IP address that is not assigned to any organization or individual, and this part of the IP address is not available on the Internet, and such IP addresses are called private IP addresses. Why can't private IP addresses be used on the Internet? Because there are no routes for private IP addresses on the Internet. The private IP address ranges include:

Class A: 10.0.0.0/8

Class B: 172.16.0.0/12 that is 172.16.0.1-172.31.255.254 a total of 16 B-type network

Class C: 192.168.0.0/16 that is 192.168.0.1-192.168.255.254 a total of 256 class C network

How to calculate IP address and CIDR

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.