Here is how it works and some example of how you use AND & OR | and XOR ^ with bit masks. AND is true is both bits AND-ED together are true, otherwise it is false. OR is true if either of the bits OR-ED together are true otherwise it is false. XOR (Exclusive OR )is true if only one of bits XOR-ED together is true. It is false if both bits are true or both bits are false. Although XOR might seem a little odd it has some particularly interesting properties.

`print "Basic binary key bit numbers\n";
print "$_\t", dec_to_bin_text($_), "\n" for qw ( 0 1 2 4 8 16 32 64 12
+8 255 );
print "\nSubnet Mask binary\n";
print "$_\t", dec_to_bin_text($_), "\n" for qw ( 255 254 252 248 240 2
+24 192 128 0);
print "\nWhat binary OR does - the | operator\n";
print do_or( 255, $_ ) for qw( 0 1 2 4 8 16 32 64 128 255 );
print do_or( 0, $_ ) for qw( 0 1 2 4 8 16 32 64 128 255 );
print "\nWhat binary AND does - the & operator\n";
print do_and( 255, $_ ) for qw( 0 1 2 4 8 16 32 64 128 255 );
print do_and( 0, $_ ) for qw( 0 1 2 4 8 16 32 64 128 255 );
print "\nWhat binary XOR does - the ^ operator\n";
print do_xor( 255, $_ ) for qw( 0 1 2 4 8 16 32 64 128 255 );
print do_xor( 0, $_ ) for qw( 0 1 2 4 8 16 32 64 128 255 );
print "\nUsing OR to set a bit in a bit mask to true\n";
print do_or( 0, 64 );
print do_or( 128, 64 );
print do_or( 255, 64 );
print "\nUsing XOR and AND to set a bit in a bit mask for false\n";
print "We want to set the 64 bit 0100000 and use the 255 XOR 64\n";
print "Which is 10111111 = 191 as a mask to do this\n";
print do_and( 255, 255^64 );
print do_and( 0, 255^64 );
print "\nUsing AND to get the value of a bit in a bit mask ( res = zer
+o if not set, >0 if set )\n";
print do_and( 0, 64);
print do_and( 255, 64);
print "\nThe interesting property of double XOR against a bit mask - b
+it fliping\n";
print "This can be the basis or a simple encryption routine as in Acti
+ve State Perl App\n";
print "\nExample 1\n";
print do_xor( 255, 10 );
print do_xor( 255, 245 );
print "\nExample 2\n";
print do_xor( 123, 10 );
print do_xor( 123, 113 );
print "\n\nThere you go!\n";
sub do_or {
my ( $b1, $b2 ) = @_;
my $res = $b1 | $b2;
$b1_txt = dec_to_bin_text($b1);
$b2_txt = dec_to_bin_text($b2);
$res_txt = dec_to_bin_text($res);
return "
$b1_txt = $b1
$b2_txt = $b2
---OR---
$res_txt = $res
";
}
sub do_xor {
my ( $b1, $b2 ) = @_;
my $res = $b1 ^ $b2;
$b1_txt = dec_to_bin_text($b1);
$b2_txt = dec_to_bin_text($b2);
$res_txt = dec_to_bin_text($res);
return "
$b1_txt = $b1
$b2_txt = $b2
--XOR---
$res_txt = $res
";
}
sub do_and {
my ( $b1, $b2 ) = @_;
my $res = $b1 & $b2;
$b1_txt = dec_to_bin_text($b1);
$b2_txt = dec_to_bin_text($b2);
$res_txt = dec_to_bin_text($res);
return "
$b1_txt = $b1
$b2_txt = $b2
--AND---
$res_txt = $res
";
}
sub dec_to_bin_text {
my $dec = shift;
my $bin_str = sprintf "%08b", $dec;
return $bin_str;
}
__DATA__
Basic binary key bit numbers
0 00000000
1 00000001
2 00000010
4 00000100
8 00001000
16 00010000
32 00100000
64 01000000
128 10000000
255 11111111
Subnet Mask binary
255 11111111
254 11111110
252 11111100
248 11111000
240 11110000
224 11100000
192 11000000
128 10000000
0 00000000
What binary OR does - the | operator
11111111 = 255
00000000 = 0
---OR---
11111111 = 255
11111111 = 255
00000001 = 1
---OR---
11111111 = 255
11111111 = 255
00000010 = 2
---OR---
11111111 = 255
11111111 = 255
00000100 = 4
---OR---
11111111 = 255
11111111 = 255
00001000 = 8
---OR---
11111111 = 255
11111111 = 255
00010000 = 16
---OR---
11111111 = 255
11111111 = 255
00100000 = 32
---OR---
11111111 = 255
11111111 = 255
01000000 = 64
---OR---
11111111 = 255
11111111 = 255
10000000 = 128
---OR---
11111111 = 255
11111111 = 255
11111111 = 255
---OR---
11111111 = 255
00000000 = 0
00000000 = 0
---OR---
00000000 = 0
00000000 = 0
00000001 = 1
---OR---
00000001 = 1
00000000 = 0
00000010 = 2
---OR---
00000010 = 2
00000000 = 0
00000100 = 4
---OR---
00000100 = 4
00000000 = 0
00001000 = 8
---OR---
00001000 = 8
00000000 = 0
00010000 = 16
---OR---
00010000 = 16
00000000 = 0
00100000 = 32
---OR---
00100000 = 32
00000000 = 0
01000000 = 64
---OR---
01000000 = 64
00000000 = 0
10000000 = 128
---OR---
10000000 = 128
00000000 = 0
11111111 = 255
---OR---
11111111 = 255
What binary AND does - the & operator
11111111 = 255
00000000 = 0
--AND---
00000000 = 0
11111111 = 255
00000001 = 1
--AND---
00000001 = 1
11111111 = 255
00000010 = 2
--AND---
00000010 = 2
11111111 = 255
00000100 = 4
--AND---
00000100 = 4
11111111 = 255
00001000 = 8
--AND---
00001000 = 8
11111111 = 255
00010000 = 16
--AND---
00010000 = 16
11111111 = 255
00100000 = 32
--AND---
00100000 = 32
11111111 = 255
01000000 = 64
--AND---
01000000 = 64
11111111 = 255
10000000 = 128
--AND---
10000000 = 128
11111111 = 255
11111111 = 255
--AND---
11111111 = 255
00000000 = 0
00000000 = 0
--AND---
00000000 = 0
00000000 = 0
00000001 = 1
--AND---
00000000 = 0
00000000 = 0
00000010 = 2
--AND---
00000000 = 0
00000000 = 0
00000100 = 4
--AND---
00000000 = 0
00000000 = 0
00001000 = 8
--AND---
00000000 = 0
00000000 = 0
00010000 = 16
--AND---
00000000 = 0
00000000 = 0
00100000 = 32
--AND---
00000000 = 0
00000000 = 0
01000000 = 64
--AND---
00000000 = 0
00000000 = 0
10000000 = 128
--AND---
00000000 = 0
00000000 = 0
11111111 = 255
--AND---
00000000 = 0
What binary XOR does - the ^ operator
11111111 = 255
00000000 = 0
--XOR---
11111111 = 255
11111111 = 255
00000001 = 1
--XOR---
11111110 = 254
11111111 = 255
00000010 = 2
--XOR---
11111101 = 253
11111111 = 255
00000100 = 4
--XOR---
11111011 = 251
11111111 = 255
00001000 = 8
--XOR---
11110111 = 247
11111111 = 255
00010000 = 16
--XOR---
11101111 = 239
11111111 = 255
00100000 = 32
--XOR---
11011111 = 223
11111111 = 255
01000000 = 64
--XOR---
10111111 = 191
11111111 = 255
10000000 = 128
--XOR---
01111111 = 127
11111111 = 255
11111111 = 255
--XOR---
00000000 = 0
00000000 = 0
00000000 = 0
--XOR---
00000000 = 0
00000000 = 0
00000001 = 1
--XOR---
00000001 = 1
00000000 = 0
00000010 = 2
--XOR---
00000010 = 2
00000000 = 0
00000100 = 4
--XOR---
00000100 = 4
00000000 = 0
00001000 = 8
--XOR---
00001000 = 8
00000000 = 0
00010000 = 16
--XOR---
00010000 = 16
00000000 = 0
00100000 = 32
--XOR---
00100000 = 32
00000000 = 0
01000000 = 64
--XOR---
01000000 = 64
00000000 = 0
10000000 = 128
--XOR---
10000000 = 128
00000000 = 0
11111111 = 255
--XOR---
11111111 = 255
Using OR to set a bit in a bit mask to true
00000000 = 0
01000000 = 64
---OR---
01000000 = 64
10000000 = 128
01000000 = 64
---OR---
11000000 = 192
11111111 = 255
01000000 = 64
---OR---
11111111 = 255
Using XOR and AND to set a bit in a bit mask for false
We want to set the 64 bit 0100000 and use the 255 XOR 64
Which is 10111111 = 191 as a mask to do this
11111111 = 255
10111111 = 191
--AND---
10111111 = 191
00000000 = 0
10111111 = 191
--AND---
00000000 = 0
Using AND to get the value of a bit in a bit mask ( res = zero if not
+set, >0 if set )
00000000 = 0
01000000 = 64
--AND---
00000000 = 0
11111111 = 255
01000000 = 64
--AND---
01000000 = 64
The interesting property of double XOR against a bit mask - bit flipin
+g
This can be the basis or a simple encryption routine as in Active Stat
+e Perl App
Example 1
11111111 = 255
00001010 = 10
--XOR---
11110101 = 245
11111111 = 255
11110101 = 245
--XOR---
00001010 = 10
Example 2
01111011 = 123
00001010 = 10
--XOR---
01110001 = 113
01111011 = 123
01110001 = 113
--XOR---
00001010 = 10
There you go!
`

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Comment onRe: Re: What A Wonderful World: Bitmasks!DownloadCode