in reply to behavior of bitwise shift

Not directly an answer, but check out Net::Netmask which already handles this for you.

Replies are listed 'Best First'.
Re^2: behavior of bitwise shift
by Taulmarill (Deacon) on Oct 25, 2004 at 14:53 UTC
    just a quic hack how subnets can be translated without using a module
    my $cidr = 8; my $mask = "1" x ( 32 - $cidr ) . "0" x $cidr; print $mask, "\n"; $mask = join ".", ( unpack "C4", ( pack "B*", $mask ) ); print $mask, "\n"

    i also suggest Net::Subnets for working with subnets if it does what you want, in particular matching ip's against subnets is very fast.
      I think you'll need to change
      my $mask = "1" x ( 32 - $cidr ) . "0" x $cidr;
      to
      my $mask = "1" x $cidr . "0" x ( 32 - $cidr );
      since /8 means a mask of 255.0.0.0 and not 255.255.255.0 which is /24.
      But thanks a lot for this example - I was wondering how do that without a module (and could not figure how bitwise shifting should help).
        oh, yes, you are abviously right.
        i was so preoccupied remembering what pack/unpack modes i have to use that i just missed that.