in reply to Bitwise Complement

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