I'm having trouble with some bitwise operations. Primarily ^ and ~. I don't seem to have issues with & and |. I was wondering if someone could help me understand what is going wrong here. Much thanks if you can. I'm attaching the applicable code and the results.
I can't understand why when complement is used perl will not return a binary string to me as above with the &. It also doesn't seem to do ^ operations properly too. I'm sure this is my fault but after reading Perl docs and message boards for a bit I can't figure it out.
The output is below.
icarus# ./test.pl
01001000000111110100111100000000
þÿþþÿþþþþþþÿÿÿÿÿþÿþþÿÿÿÿÿÿÿÿÿÿÿÿ
The small test program is below.
#!/usr/bin/perl
use Validator;
my $suspect = '72.31.79.5';
my $suspect2 = '255.255.255.0';
$obj = Validator->new();
$obj->is_hostaddress($suspect, $suspect2);
The subroutine from the module is below.
sub is_hostaddress {
my $self = shift if ref($_[0]);
my $ipaddr = shift;
my $netmask = shift;
return unless defined($ipaddr) && defined($netmask);
my $binipaddr = unpack('B32', pack('C4C4C4C4', split(/\./, $ip
+addr)));
my $binnetmask = unpack('B32', pack('C4C4C4C4', split(/\./, $n
+etmask)));
my $result = $binipaddr & $binnetmask;
my $result2 = $binipaddr | ~ $binnetmask;
print $result;
print "\n";
print $result2;
print "\n";
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.