Bitwise operator - what am I doing wrong??

S

Suk

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 ?
 
D

Dr.Ruud

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";
 
S

Suk

Dr.Ruud said:
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";

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]
?
 
I

Ingo Menger

Suk said:
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.
 
D

Dr.Ruud

Suk schreef:
Dr.Ruud:
#!/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"
'
 
J

John W. Krahn

Suk said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top