Calculate PREFIX from subnet mask in bootscripts

Ian Molton spyro at f2s.com
Tue Oct 19 08:42:57 PDT 2004


Gerard Beekmans wrote:

> So never mind my question about what 255.255.255.252 is. It's /30
> according to this table. Good enough for me. Though I'd still appreciate
> the math behind it if anybody can explain it.

Yeah its dead easy.

the subnet mask divides the ip into network and host addresses

if you AND the mask with the ip you get the network address (sorta)

if you and the inverse of the mask with the IP you get the host address 
on that network.

so, taking each digit in turn

ip: 124.201.84.15
nm: 255.255.255.248  (this is a /29)

this is, in binary:

ip: 01111100.11001001.01010100.00001111
nm: 11111111.11111111.11111111.11111000

so, ANDing the ip and nm give us a network addr:

01111100.11001001.01010100.00001000

although really we discard the non network bits, leaving:

01111100.11001001.01010100.00001 or
01111100110010010101010000001 or
261696129

and a host number of:
00000000.00000000.00000000.00000111 or
7



More information about the lfs-dev mailing list