netrange.pl <network base/CIDR notation> (e.g. 192.168.1.0/25 is half of a private class C). Returns the number of hosts, netmask and broadcast address.
#!/usr/bin/perl @VALUE = split (/\//, $ARGV[0]); @OCTETS = split (/\./, $VALUE[0]); $calculated = 2**(32 - $VALUE[1]); $OCTETS[3]--; # (base counts as one...) $OCTETS[3] += $calculated; $broadcast = join (".", @OCTETS); print qq~ $calculated hosts Base is $VALUE[0] Broadcast is $broadcast~;

Replies are listed 'Best First'.
Re: My network range
by atcroft (Abbot) on Dec 08, 2001 at 11:57 UTC

    Where you have

    $calculated = 2**(32 - $VALUE[1]);
    you might try this instead (especially if this code gets performed over and over and ove...) :
    $calculated = 1 << (32 - $VALUE[1]);

    A bit-wise shift, especially in the case where you're dealing with powers of two, would likely be much less computationally expensive that exponentiation via multiplication. (That's my guess-can someone verify this?)