Some of the operators don't work as well when the values they are acting on are represented as binary strings. If we borrow some code from the Perl cookbook (their decimal/binary conversion functions, we can illustrate the issue:
#!/usr/bin/perl use warnings; use strict; package main; my $suspect = '72.31.79.5'; my $suspect2 = '255.255.255.0'; &is_hostaddress($suspect, $suspect2); sub dec2bin { my $str = unpack("B32", pack("N", shift)); return $str; } sub bin2dec { return unpack("N", pack("B32", substr("0" x 32 . shift, -32))); } sub is_hostaddress { 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))); print "ip addr = $binipaddr\n"; print "netmask = $binnetmask\n"; # # Convert the mask back to a decimal integer before # attempting to negate. Negating directly gives # Interesting results. # my $ugly = ~ $binnetmask; my $not = dec2bin( ~ bin2dec($binnetmask)); print "badmask = $ugly\n"; print "notmask = $not\n"; my $result = $binipaddr & $binnetmask; my $result2 = $binipaddr | $not; print $result; print "\n"; print $result2; print "\n"; }
The results being:

C:\Code>perl masktest.pl ip addr = 01001000000111110100111100000101 netmask = 11111111111111111111111100000000 badmask = ¨p¨p¨p¨p¨p¨p¨p¨p¨p¨p¨p¨p¨p¨p¨p¨p¨p¨p¨p¨p¨p¨p¨p¨p¨k¨k¨k¨k¨k¨k +¨k¨k notmask = 00000000000000000000000011111111 01001000000111110100111100000000 01001000000111110100111111111111

In reply to Re: Bitwise Complement by dwm042
in thread Bitwise Complement by definitemaybe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.