Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

To get to your broadcast address you'd have to add the binary representations of your ip and corresponding netmask, true? Using perl's & operator this should be feasible with something like this simple-minded approach:
for (0..3){ $broadcast[$_] = $ip[$_] & $netmask[$_] }
But no, wide away, issues of precedence or context?

How would you do it? Thanks for your help!

Replies are listed 'Best First'.
Re: Broadcast address: a binary anding question
by quidity (Pilgrim) on Nov 24, 2000 at 16:53 UTC

    CPAN is, as ever, your friend. I would use the Net::IPv4Addr module. From the synopsis:

    use Net::IPv4Addr qw( :all ); my ($broadcast) = ipv4_broadcast($ip_str, $msk_str);

    Where $ip_str and $msk_str in this case (assuming you have (0..255) in your arrays) would be:

    $ip_str = join('.', @ip); $msk_str = join('.', @netmask);

    If you want to know how your problem is solved in the module, then download it and use the source luke.