in reply to Re: behavior of bitwise shift
in thread behavior of bitwise shift

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.

Replies are listed 'Best First'.
Re^3: behavior of bitwise shift
by Golo (Friar) on Oct 25, 2004 at 15:11 UTC
    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.