Search code examples
cidr

CIDR and number of ip-addresses


I read 10.240.0.0/24 can host to up 254 ip-addresses. How?
How do I intuitively understand what /24 is doing here to help give 254 unique ip-addresses?


Solution

  • TL; DR;

    A short way to compute the number of hosts would be

    2 ^ ( 32 - 24 ) - 2 = 256
    

    Because:

    1. We are doing bit operations (0, 1, two possible values)
    2. An IP is a set of 4 octet, when an octet is 8 bits (4 * 8 = 32)
    3. 24 is your CIDR
    4. There is two reserved IP on every subnet, the broadcast address and the subnet zero, they shouldn't be used for hosts

    CIDR is computed with the help of bitwise operations.

    An IP is a set of 4 octet, each separated with a dot.

    255.255.255.255
    =
    11111111.11111111.11111111.11111111
    

    When you specify a CIDR of /24 you are asking for a subnet for your IPs with a mask that would be padded with 24 bits set to 1

    11111111.11111111.11111111.00000000
    =
    255.255.255.0
    

    Your IP is

    10.240.0.0
    =
    00001010.11110000.00000000.00000000
    

    Now we can apply a bitwise AND between your IP and your subnet

    11111111.11111111.11111111.00000000
    &
    00001010.11110000.00000000.00000000
    =
    00001010.11110000.00000000.00000000
    

    So you end up with 10.240.0.0 being your IP prefix.
    The same subnet could be applied to subsequent IPs

    10.240.0.1

    11111111.11111111.11111111.00000000
    &
    00001010.11110000.00000000.00000001
    =
    00001010.11110000.00000000.00000000
    

    Giving the same 10.240.0.0 IP prefix

    10.240.0.2

    11111111.11111111.11111111.00000000
    &
    00001010.11110000.00000000.00000010
    =
    00001010.11110000.00000000.00000000
    

    Giving the same 10.240.0.0 IP prefix
    And so on, and so forth

    All in all, the bitwise operation is pretty straight forward:

    • each time you have a 0 & x it will equal 0
    • each time you have a 1 & x it will equal x

    So that means that with 10.240.0.0/24, you have a subnet of 255.255.255.0 and so a range of IP from 10.240.0.0 up to 10.240.0.255.

    That still gives you 256 possible addresses you would say?

    Well, yes, but you have to remember that in IPv4, you have two addresses that are not usable:

    • the subnet zero (the first address of your range)
    • and the broadcast address (the last address of your range)

    Special Addresses:

    From the Assigned Numbers memo [Reynolds, J., and J. Postel, "Assigned Numbers", RFC-943, USC/Information Sciences Institute, April 1985.]:

    "In certain contexts, it is useful to have fixed addresses with functional significance rather than as identifiers of specific hosts. When such usage is called for, the address zero is to be interpreted as meaning "this", as in "this network". The address of all ones are to be interpreted as meaning "all", as in "all hosts". For example, the address 128.9.255.255 could be interpreted as meaning all hosts on the network 128.9. Or, the address 0.0.0.37 could be interpreted as meaning host 37 on this network."

    It is useful to preserve and extend the interpretation of these special addresses in subnetted networks. This means the values of all zeros and all ones in the subnet field should not be assigned to actual (physical) subnets.

    Source: https://www.ietf.org/rfc/rfc950.txt

    So now, if you do 256 - 2, you have your 254 available hosts.

    To sum up:

    • CIDR: 10.240.0.0/24
    • Subnet mask: 255.255.255.0 (24 times a 1 when the IP is shown as groups of octet)
    • IP range: 10.240.0.0 - 10.240.0.255
    • Subnet zero: 10.240.0.0
    • Broadcast address: 10.240.0.255
    • Hosts IP range:10.240.0.1 - 10.240.0.254