#!/usr/bin/perl -w use strict; # slight modification to our output template to accomodate the fact # that "bits" is longer than two characters... printf "%-8s %-15s %-35s %-4s %-10s\n", qw(hex dec bin bits hosts); printf "%-8s %-15s %-35s %-4s %-10s\n", "-" x 8, "-" x 15, "-" x 35, "-" x 4, "-" x 10; for (my $host_bits = 31; $host_bits >= 0; --$host_bits) { # the network mask is all ones, but shifted over by the number of # host bits. my $net_mask = (~0 << $host_bits) & 0xffffffff; # and the hex value is just as easy: my $hex_rep = sprintf "%08x", $net_mask; # i end up using this in a few places, so let's break the mask into # individual bytes... my @bytes = unpack "CCCC", pack "N", $net_mask; # the dotted-decimal version is pretty simple ... my $dec_rep = sprintf "%3d.%3d.%3d.%3d", @bytes; # the binary representation is a bit hairier, but to borrow veblen's # construction, we have: my $bin_rep = join " ", map { unpack "B*", chr($_) } @bytes; # the number of hosts is 2**(bits available for hosts), less two: # ...0 and ...1 are reserved for broadcast addresses. according to # veblen, the real difference is that ....0000 is the network ID, # and ...1111 is the broadcast addr within that network ID. my $n_hosts = 2**$host_bits - 2; $n_hosts = 0 if $n_hosts < 0; printf "%s %s %s %2d %10d\n", $hex_rep, $dec_rep, $bin_rep, $host_bits, $n_hosts; }