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?
A short way to compute the number of hosts would be
2 ^ ( 32 - 24 ) - 2 = 256
Because:
0
, 1
, two possible values)4 * 8 = 32
)24
is your CIDRCIDR 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:
0 & x
it will equal 0
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:
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:
10.240.0.0/24
255.255.255.0
(24 times a 1
when the IP is shown as groups of octet)10.240.0.0
- 10.240.0.255
10.240.0.0
10.240.0.255
10.240.0.1
- 10.240.0.254