# How to calculate IP address _ipv4

Source: Internet
Author: User

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 usually represented in 4 groups of 3-bit decimal digits, with "." In the middle. Separated. For example, 192.168.0.1.

Add (IPV6): The previous 32-bit IP address called IPV4, with the development of information technology, IPV4 the number of available IP addresses can not meet people's daily needs, according to the authority to predict the 2010 to fully apply information technology, everyone needs at least 10 IP addresses, such as: computer, Laptops, mobile phones, and intelligent refrigerators. In order to solve this problem developed the IPV6 specification, IPV6 uses 128 bits to represent the IP address, it is represented as 8 Group 4 bits 16 in number, the middle is ":" Separates. For example, Ab32:33ea:89dc:cc47:abcd:ef12:abcd:ef12.

To facilitate IP addressing by dividing IP addresses into categories A, B, C, D, and E, each IP address provides a clear indication of the number of digits used in each IP address to represent the network ID and host ID. When the number of digits of the host ID is determined, the number of computers that can be included in a network is determined, and users can choose a type of IP address to construct the network structure flexibly according to the needs of the enterprise.

Class A

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

Class B

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

Class C

Class C addresses use the IP address of the first 24 digits to represent the network ID, with the IP address 8 digits to represent the host ID. Class C addresses are used to indicate that the first three bits of the network ID must start with 110, the other 22 bits can be any value, when the other 22 bits are all 0 is the minimum network ID, the first byte of the IP address is 192; When the other 22 bits are all 1 o'clock the network ID is the largest, the first byte is the largest, that is 223. Class C IP Address the valid range for the first byte is 192-223, a total of 2,097,152 C-class networks; Each Class C network can contain 28-2 hosts (that is, 254 hosts).

Class D

Class D address for multicast use, without network ID and host ID, the first four bits of the class D IP address must start with 1110, and the other 28 bits can be any value, then the valid range for Class D IP addresses is 224.0.0.0 to 239.255.255.255.

Class E

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

In practical applications, only a, B, and C three IP addresses can be assigned directly to the host, Class D and Class 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, and the computer with the same network ID does not need to be able to communicate directly through a router connection, and we make a network with the same network ID as a local network (network segment); Communication between computers with different network IDs must be connected through a router. We call 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, and which part represents the host ID, is not determined by the class that the IP address belongs to, but is determined by the subnet mask. Subnets determine which subnet an IP address belongs to.

The subnet mask is formatted with a continuous 255 followed by a sequential 0, where the contiguous 255 represents the network ID, and a sequential 0 component represents 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, there are five kinds of subnet masks, 0.0.0.0, 255.0.0.0, 255.255.0.0, 255.255.255.0 and 255.255.255.255. Subnet masks in this format the number of hosts in each network varies by at least 256 times times, which is not conducive to flexible allocation of IP addresses according to the needs of the enterprise. For example, a business has 2000 computers, Either the user assigns a subnet mask of 255.255.0.0, the network can contain 65534 computers, it will waste 63,534 IP addresses, or if the user assigns 8 255.255.255.0 networks to it, then the network management and maintenance burden must be attached to this 8 network by a router.

The network ID is the IP address and the subnet mask is obtained with the operation, the IP address of the part that represents the host ID into 0, indicating that the network ID 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.

Network id:192.168.0.0 host id:23.35

Network id:192.168.23.0 host Id:35

Iv. Subnet and CIDR

By converting a regular subnet mask to binary, you will find that the subnet mask is contiguous binary 1 and continuous 0, where 1 of the subnet mask represents the network ID and the host ID of 0 in the subnet mask. For example, 255.255.0.0 is converted to binary 11111111 11111111 00000000 00000000.

In the previous example, why not use a sequential 1 part to represent the network ID, and 0 consecutive parts represent the host ID. The answer is yes, IP addressing technology using this approach is called a class-free inter-domain routing (CIDR). CIDR technology uses 1 consecutive parts of the subnet mask to represent the network ID, and 0 consecutive parts represent the host ID. For example, the network contains 2000 computers, only 11 bits to represent the host ID, 21-bit table network ID, the subnet mask is 11111111.11111111 11100000.00000000, and the conversion to decimal is 255.255.224.0. At this point, the network will contain 2046 of computers, will not cause the waste of IP address, and will not use routers to connect the network, increase the amount of additional management maintenance.

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

Case 1:192.168.23.35/21

Subnet mask: 11111111 11111111 11111000 00000000 255.255.248.0

Network id:192.168.00010111.0 (where the third byte of the red section represents the network ID, the other represents the host ID, the network ID is the network ID part is maintained unchanged the host ID is all changed to 0) the network ID is 192.168.16.0

Start IP Address: 192.168.16.1 (host ID cannot be all 0, all 0 means network ID last 1)

End IP Address: 192.168.00010111.11111110 (host ID cannot be all 1, all 1 for local broadcast) ends the IP192.168.23.254.

Example 2: Divides 163.135.0.0 into 16 subnets, calculating the network IDs, subnet masks, and starting and ending IP addresses of the first two subnets.

1th step: With CIDR representation 163.135.0.0/20, 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

3rd Step: Second network id:163.135.16.0

Five, subnet mask and network ID fast calculation method

CIDR's subnet masks are contiguous 1 and 0 of the connections, and the subnet masks have the following representations:

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

We all know that the decimal number of 11111111 is 255, so how do we compute the subnet mask quickly? Binary 1=1,11=3,111=7,1111=15, then 1111 1110=255-1,1111 1100=255-3,1111 1000=255-8,1111 0000=255-15 This is not very soon. As soon as we determine how many bits in the subnet mask represent the network ID, we can immediately write the subnet mask. 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 don't need to remember how much each one is, just do a simple addition and subtraction to fix the subnet mask calculation.

The results of the network ID Everyone knows that the network ID part of the same, the host ID partially changed to 0, then in the calculation of network ID, first look at the subnet mask in the number of bits used to represent the network, the corresponding in the IP address to binary conversion only the front several, such as 192.168.176.15/ 19, the network ID is 19 bits, then the first two bytes of the network ID are 192.168.x.0 and the third byte is changed. So how do you quickly figure out the value of this variable x? We know that the third byte only three digits to represent the network ID, the conversion time 176>128, the 1th bit is 1,176-128=48<64, the 2nd bit is the 0,48>32 3rd bit is 1, the remaining computation has no meaning, all wants to convert to 0, the network ID is 10100000, The network ID is 192.168.160.0, so the likelihood of a calculation error is small.

VI. local and remote network concepts

Computers with the same network ID are called local networks, and computers in the local network do not need routers to communicate with each other; computers with different network IDs are called remote networks, and computers in remote networks must communicate with each other through routers.

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 0 and the host ID is all 1 of the illegal IP address: 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.

The same network ID belongs to the same network segment, then 192.168.10.16/28 does not belong to the same network segment.

Seven, subnet number and host number calculation method

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

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

Because the IP address is 32 bits and the network ID is represented by 20 digits, the number of digits 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 by 2 when you compute the IP address? 2. The reason for the IP address minus 2 is that the host ID cannot be 0 or all 1; Subnets do not have this problem.

Viii. public IP and private IP addresses

IP addresses are managed and assigned by the IANA (Internet address allocation authority) and must be allocated by the IANA for any IP address to be available on the Internet, and the IANA-assigned IP address that is normally used on the internet is called a public IP address The IANA retains a portion of the IP address that is not assigned to any organization or individual, which is not available on the Internet, which is called a private IP address. Why are private IP addresses not available on the Internet? Because there are no routes for private IP addresses on the Internet. Private IP address ranges include:

Class A: 10.0.0.0/8

Class B: 172.16.0.0/12 172.16.0.1-172.31.255.254 A total of 16 B-class networks

Class C: 192.168.0.0/16-192.168.0.1-192.168.255.254 A total of 256 C-class networks

Related Keywords:

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.