my ($addr, $bits, $network, $mask) = split("/", $cidr); $network = $network * 256 + $_ foreach(split(/\./, $addr); $mask = eval '0b'.('1' x $bits).('0' x (32 - $bits)); print int2ip($network & $mask)."\t".int2ip($mask)."\n";
(the int2ip function was the same as that the original poster had used, so I don't bother reproducing it here)
The string eval - abhorred by many - meant I could avoid several lines of fairly opaque bit-banging and I think it's *very* clear what that line does, and it also makes it obvious how netmasks are related to the /bits in CIDR notation.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: String eval is cool
by salva (Canon) on Jun 22, 2006 at 12:39 UTC | |
|
Re: String eval is cool
by ikegami (Patriarch) on Jun 22, 2006 at 14:06 UTC | |
by DrHyde (Prior) on Jun 27, 2006 at 08:52 UTC | |
by ikegami (Patriarch) on Jun 27, 2006 at 14:16 UTC | |
|
Re: String eval is cool
by jwkrahn (Abbot) on Jun 23, 2006 at 07:12 UTC | |
by ikegami (Patriarch) on Jun 23, 2006 at 14:41 UTC | |
|
Re: String eval is cool
by shmem (Chancellor) on Jun 22, 2006 at 21:39 UTC | |
|
Re: String eval is cool
by ikegami (Patriarch) on Jun 22, 2006 at 21:53 UTC |