Be careful though, since perl doesn't have unsigned 32 bit integers, some IP addies might be large enough to flip the high bit on,
don't know if this would be a problem or not....


Tony

$Bill Luebkert wrote:

Viswanatha Rao wrote:



Hello,



I have to perform a bitwise AND operation on an IP address and a subnet
mask, to generate a network number and host ID. Can anyone tell me how
to do this?



Try something like:

use strict;

my @ip = split /\./, '192.168.0.1';

my $ip = 0;
$ip = ($ip << 8) | $_ foreach @ip;

my $mask = 0xffff0000;
my $net = $ip & $mask;

printf "ip=%u (%08x), mask=%u (%08x), net %u (%08x)\n", $ip, $ip, $mask, $mask,
 $net, $net;

__END__





--

SKYKING, SKYKING, DO NOT ANSWER.


_______________________________________________ ActivePerl mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to