Calculate PREFIX from subnet mask in bootscripts

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

Gerard Beekmans wrote:

> So never mind my question about what 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

nm:  (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:


although really we discard the non network bits, leaving:

01111100.11001001.01010100.00001 or
01111100110010010101010000001 or

and a host number of:
00000000.00000000.00000000.00000111 or

More information about the lfs-dev mailing list