Perl Monk, Perl Meditation PerlMonks

### Re: Re: What A Wonderful World: Bitmasks!

by tachyon (Chancellor)
 on Dec 08, 2002 at 17:44 UTC ( #218379=note: print w/replies, xml ) Need Help??

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 "\$_\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

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

Replies are listed 'Best First'.
Re: Re: Re: What A Wonderful World: Bitmasks!
by sauoq (Abbot) on Dec 09, 2002 at 05:15 UTC

Good node but woefully incomplete! How could you forget bitwise negation? It's really rather useful with bitmasks. For instance, \$flags &= ~\$foo_flag is a common idiom for flipping a bit off.

I'll pick a nit while I'm at it. Throughout your node you refer to "binary AND", "binary OR", and "binary XOR". Strictly speaking, you mean "bitwise" rather than "binary." A binary operator is an operator that takes two operands. Both logical and bitwise AND are binary operators. For contrast, consider unary operators such as numerical, logical, and bitwise negation which take one operand as well as the trinary operator (?:) which takes three.

Just the same, ++ for the otherwise thorough explanation.

```-sauoq
"My two cents aren't worth a dime.";
```
Re: Re: Re: What A Wonderful World: Bitmasks!
by demerphq (Chancellor) on Dec 08, 2002 at 23:20 UTC
Excellent node. I completely forgot to cover the operators.

One thing id like to add however is that your comment about XOR being the basis of simple encryption, while being true, should not be emulated. XOR based cyphers are particularly vulnerable to being cracked. Also, the most common use of XOR is right in front of everybodys eyes: graphics.

--- demerphq
my friends call me, usually because I'm late....

I suppose you don't like rot13 either ;-)

cheers

tachyon

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

I think XOR is still at the base of modern symetric (i.e. private key) encryption algorithms. They create a stream which is XORed with the plaintext. The difference between the simple XOR encryption of which you are surely thinking and these better algorithm is that the stream these new algorithms create is not periodic.

If I remember correctly, one end of an HTTPS connection uses public key encryption to distribute a private key to its peer, and then they switch to symetric (private key) encryption. It does this because symetric is faster, and XOR surely plays a part in that.

Feel free to correct me. This subject is far from fresh in my mind.

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://218379]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2021-09-16 23:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?