in reply to bitwise operators

Not your original question, but you might check out Net::Netmask which handles this stuff (including "is this IP in this mask?").

Replies are listed 'Best First'.
Re^2: bitwise operators
by Anonymous Monk on Jan 19, 2005 at 21:11 UTC
    use Socket; $net = "192.168.0.0"; $mask = "255.255.255.240"; $ip = "192.168.0.5"; print "$ip belongs to $net/$mask\n" if ((net2long($ip) & net2long($mas +k)) == net2long($net)); sub net2long { return unpack("N", inet_aton(shift)); }

    2005-01-20 Janitored by Arunbear - added code tags, as per Monastery guidelines

      And for CIDR notation use this :
      use Socket; $net = "192.168.0.0"; $mask = "28"; # /28 is 255.255.255.240 $ip = "192.168.0.5"; print "$ip belongs to $net/$mask\n" if ((net2long($ip) & ~((1 << (32 - + $mask)) - 1 )) == net2long($net)); sub net2long { return unpack("N", inet_aton(shift)); }

      2005-01-20 Janitored by Arunbear - added code tags, as per Monastery guidelines