Mickey Perlstein
Mickey Perlstein

Reputation: 4044

How to calculate number of hosts between two ips? c#

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?

Upvotes: 3

Views: 4525

Answers (2)

Rick Su
Rick Su

Reputation: 16440

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.

Upvotes: 1

Guffa
Guffa

Reputation: 700362

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).

Upvotes: 8

Related Questions