in reply to bitwise operators

Make sure your arguments are numbers, not strings:
print 161 & 255; # Prints 161 print "161" & "255"; # Prints 041
You probably use variables, so you have to make sure Perl thinks they contain only numbers, and not strings. If you populate your variables by reading it from file, or by extracting it with a regex, Perl thinks the variables are strings. You can numify them by adding 0s:
print +("161" + 0) & ("255" + 0); # Prints 161

Replies are listed 'Best First'.
Re^2: bitwise operators
by bart (Canon) on Jan 19, 2005 at 17:20 UTC
    If only one of them is a number and the other a string, both will be treated as integers.
    $x = "161"; # a string print $x & 255; # the other one is a number...
    Result:
    161
    

    Assuming the "161" comes from user input and the 255 is hardcoded, there's no reason to have a problem.

      No, but that's a big if. This is 2005, and A-, B-, C- subnets are so 1980s. If I have a /23 subnet, I won't be using 255 exclusively.
      $ ifconfig | awk 'NR == 2 {print $4}' Mask:255.255.224.0