Bitwise operator - what am I doing wrong??

Discussion in 'Perl Misc' started by Suk, Nov 15, 2006.

  1. Suk

    Suk Guest

    I'm trying to get the network number given an IP address and subnet
    mask on the command line

    Why doesnt this work?

    #!/usr/local/bin/perl
    #
    our ($ip,$netmask,@ip,@net,@nw);

    $ip=$ARGV[0];
    $netmask=$ARGV[1];

    @ip=(split(/\./,$ip));
    @net=(split(/\./,$netmask));

    $i=0;

    foreach (@ip) {
    $nw[$i]=$_ & $net[$i];
    $i++;
    }

    print "IP: $ip NETMASK: $netmask NETWORK: @nw\n";

    # ./shownet 136.19.96.178 255.255.254.0
    IP: 136.19.96.178 NETMASK: 255.255.254.0 NETWORK: 014 01 04 0

    Yet 136 & 255 on the command line yields the correct answer
    # perl
    print 136 & 255
    136

    What's going on ?
     
    Suk, Nov 15, 2006
    #1
    1. Advertising

  2. Suk

    Dr.Ruud Guest

    Suk schreef:
    > I'm trying to get the network number given an IP address and subnet
    > mask on the command line
    >
    > Why doesnt this work?
    >
    > #!/usr/local/bin/perl
    > #
    > our ($ip,$netmask,@ip,@net,@nw);
    >
    > $ip=$ARGV[0];
    > $netmask=$ARGV[1];
    >
    > @ip=(split(/\./,$ip));
    > @net=(split(/\./,$netmask));


    $_ += 0 for @net ;


    > $i=0;
    >
    > foreach (@ip) {
    > $nw[$i]=$_ & $net[$i];
    > $i++;
    > }
    >
    > print "IP: $ip NETMASK: $netmask NETWORK: @nw\n";
    >
    > # ./shownet 136.19.96.178 255.255.254.0
    > IP: 136.19.96.178 NETMASK: 255.255.254.0 NETWORK: 014 01 04 0



    #!/usr/bin/perl
    use warnings;
    use strict;

    my @ip = split /\./, my $ip = $ARGV[0] ;
    my @nm = split /\./, my $nm = $ARGV[1] ;

    my ($i, @nw) = (0) ;

    $nw[$i] = $_ & (0 + $nm[$i]), $i++ for @ip ;

    print "IP: $ip NETMASK: $nm NETWORK: @nw\n";

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Nov 15, 2006
    #2
    1. Advertising

  3. Suk

    Suk Guest

    Dr.Ruud wrote:
    > Suk schreef:
    > > I'm trying to get the network number given an IP address and subnet
    > > mask on the command line
    > >
    > > Why doesnt this work?
    > >
    > > #!/usr/local/bin/perl
    > > #
    > > our ($ip,$netmask,@ip,@net,@nw);
    > >
    > > $ip=$ARGV[0];
    > > $netmask=$ARGV[1];
    > >
    > > @ip=(split(/\./,$ip));
    > > @net=(split(/\./,$netmask));

    >
    > $_ += 0 for @net ;
    >
    >
    > > $i=0;
    > >
    > > foreach (@ip) {
    > > $nw[$i]=$_ & $net[$i];
    > > $i++;
    > > }
    > >
    > > print "IP: $ip NETMASK: $netmask NETWORK: @nw\n";
    > >
    > > # ./shownet 136.19.96.178 255.255.254.0
    > > IP: 136.19.96.178 NETMASK: 255.255.254.0 NETWORK: 014 01 04 0

    >
    >
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    >
    > my @ip = split /\./, my $ip = $ARGV[0] ;
    > my @nm = split /\./, my $nm = $ARGV[1] ;
    >
    > my ($i, @nw) = (0) ;
    >
    > $nw[$i] = $_ & (0 + $nm[$i]), $i++ for @ip ;
    >
    > print "IP: $ip NETMASK: $nm NETWORK: @nw\n";
    >
    > --
    > Affijn, Ruud
    >
    > "Gewoon is een tijger."


    hey thanks that now works, but I still dont understand what I was doing
    wrong?
    You have to set @nw to zero first ? Why did you have to do 0 + $nm[$i]
    ?
     
    Suk, Nov 15, 2006
    #3
  4. Suk

    Ingo Menger Guest

    Suk schrieb:

    > hey thanks that now works, but I still dont understand what I was doing
    > wrong?
    > You have to set @nw to zero first ? Why did you have to do 0 + $nm[$i]
    > ?


    Because bitwise operators work differently on strings and on integers.
    In your orginal program, since you got the "numbers" from the
    commandline, they still were strings.
    You can force a string to be recognized as number by doing something
    numeric with it, adding 0, for example.
     
    Ingo Menger, Nov 15, 2006
    #4
  5. Suk

    Dr.Ruud Guest

    Suk schreef:
    > Dr.Ruud:
    >> Suk:



    >>> #!/usr/local/bin/perl
    >>> #
    >>> our ($ip,$netmask,@ip,@net,@nw);
    >>>
    >>> $ip=$ARGV[0];
    >>> $netmask=$ARGV[1];
    >>>
    >>> @ip=(split(/\./,$ip));
    >>> @net=(split(/\./,$netmask));

    >>
    >> $_ += 0 for @net ;


    That would make all elements of @net numeric.


    >>
    >>
    >>> $i=0;
    >>>
    >>> foreach (@ip) {
    >>> $nw[$i]=$_ & $net[$i];
    >>> $i++;
    >>> }
    >>>
    >>> print "IP: $ip NETMASK: $netmask NETWORK: @nw\n";
    >>>
    >>> # ./shownet 136.19.96.178 255.255.254.0
    >>> IP: 136.19.96.178 NETMASK: 255.255.254.0 NETWORK: 014 01 04 0

    >>
    >>
    >> #!/usr/bin/perl
    >> use warnings;
    >> use strict;
    >>
    >> my @ip = split /\./, my $ip = $ARGV[0] ;
    >> my @nm = split /\./, my $nm = $ARGV[1] ;
    >>
    >> my ($i, @nw) = (0) ;
    >>
    >> $nw[$i] = $_ & (0 + $nm[$i]), $i++ for @ip ;
    >>
    >> print "IP: $ip NETMASK: $nm NETWORK: @nw\n";

    >
    > hey thanks that now works, but I still dont understand what I was
    > doing wrong?
    > You have to set @nw to zero first ?


    Nowhere @nw was set to zero first, check again.

    Maybe you were confused by

    my ($i, @nw) = (0) ;

    and I won't blame you for that because it's ugly.
    It sets $i to 0, but @nw remains unset.


    > Why did you have to do 0 + $nm[$i]
    > ?


    To make it numeric.


    $ perl -wle '
    print "b" & "c"
    '

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Nov 15, 2006
    #5
  6. Suk wrote:
    > I'm trying to get the network number given an IP address and subnet
    > mask on the command line
    >
    > Why doesnt this work?
    >
    > #!/usr/local/bin/perl
    > #
    > our ($ip,$netmask,@ip,@net,@nw);
    >
    > $ip=$ARGV[0];
    > $netmask=$ARGV[1];
    >
    > @ip=(split(/\./,$ip));
    > @net=(split(/\./,$netmask));
    >
    > $i=0;
    >
    > foreach (@ip) {
    > $nw[$i]=$_ & $net[$i];
    > $i++;
    > }
    >
    > print "IP: $ip NETMASK: $netmask NETWORK: @nw\n";
    >
    > # ./shownet 136.19.96.178 255.255.254.0
    > IP: 136.19.96.178 NETMASK: 255.255.254.0 NETWORK: 014 01 04 0


    $ perl -le'
    use Socket;
    my $ip = q[136.19.96.178];
    my $netmask = q[255.255.254.0];
    print "IP: $ip NETMASK: $netmask NETWORK: ", inet_ntoa( inet_aton( $ip ) &
    inet_aton( $netmask ) );
    '
    IP: 136.19.96.178 NETMASK: 255.255.254.0 NETWORK: 136.19.96.0




    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, Nov 15, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. littlehobo

    Bitwise operator question...

    littlehobo, Sep 30, 2003, in forum: C++
    Replies:
    10
    Views:
    6,085
    Ron Natalie
    Oct 1, 2003
  2. Peter Shaggy Haywood

    Re: bitwise operator on a struct

    Peter Shaggy Haywood, Jun 25, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,608
    Peter Shaggy Haywood
    Jun 25, 2003
  3. Manish_Ganvir
    Replies:
    13
    Views:
    1,634
    Keith Thompson
    Feb 14, 2005
  4. Ioannis Vranos
    Replies:
    8
    Views:
    360
    James Kanze
    Nov 14, 2008
  5. Neil

    what's wrong with the bitwise operators

    Neil, Mar 26, 2009, in forum: C Programming
    Replies:
    2
    Views:
    378
Loading...

Share This Page