How to calculate number of hosts between two ips?

2019-05-20 08:32发布

问题:

I have two ips:

1. 1.1.1.1
2. 4.4.4.4

obviously this is just an example, this is a dynamic calculator

how do i calculate number of hosts between said ips if subnet mask is irrelevant?

回答1:

To calculate the number of (theoretical) IP addresses you would convert each IP address to it's 32 bit integer format (which is actually what it really is), then it's just a matter of simple subtraction:

1.1.1.1 = 0x01010101 = 16843009
4.4.4.4 = 0x04040404 = 67372036

Number of addresses excluding the start and end address:

67372036 - 16843009 - 1 = 50529026

Number of addresses including the start and end address:

67372036 - 16843009 + 1 = 50529028

The number of actual usable addresses would be somewhat lower. Normally a few addresses in each C range is reserved for things like the gateway (router).



回答2:

IPv4 address can be represented by an integer (4 bytes, 32 bits)

To simplify the question, assuming

  • All IP addresses are usable
  • One host per IP address

Then, converting both IP addresses to integer and work out the difference would be the number of hosts available.



标签: c# algorithm ip