in reply to bitwise operators
1. Cryptography
This is a very simple example, but still very powerful.
Bitwise Operators can play a very important role in cryptographic/masking algorithms. For example, say that you had a piece of binary data, for simplicity, these 15 bytes:
This is a test.
You also have a 15 byte "mask", a string of random bytes, the same length as the length of the string to be masked. Again, for simplicity, lets say the (obviously not very random) mask is:
abcdefghijklmno
To encode the orginal data, you would apply the mask "abcdefghijklmno" to the data "This is a test." You would do this by using the XOR bitwise operation.
THE XOR OPERATOR
The XOR operator works on bits in the same ways as explained in previous responses. The XOR test is true(1) if the two bits that are being compared are different(1 and 0), or false(0) if they are the same. Therefore, performing the XOR operation on the following 2 bytes would work as shown:
10010111
01011100
-----------
11001011
Now, lets look at the last 2 bytes in the above example and XOR them:
01011100
11001011
-----------
10010111
You get the original piece of data back!
Getting back to our 15 byte example, if you XOR "This is a test." with "abcdefghijklmno", you will get a 15 byte piece of "masked data", that is hopelessly encoded and cannot be decoded by anyone without our incredibly secure and hard to guess mask. This new masked data(which is not shown here, it turns out to be ugly, hard to display binary), can be reverted back to the original by XORing it with the mask to obtain the data, or the data to obtain the mask. This is probably the most secure way to well, secure data, because since the mask is random(usually), the masked data can be just as easily be reverted back to the string "No test here..." using another mask.
2. Internal data structures
Bitwise operators can be used to manipulate data in a very efficient way that has been stored in bit fields, used in many areas including video games and 3D vector engines. It is also important in advanced mathematical algorithms. Instead of providing more, drawn out examples, I will provide you with the following sources, if you are still interested.
Mastering Algorithms with Perl -- Explains the use of bit vectors in mathematical algorithms and Encrypting data using the XOR method I discussed.
http://www.cs.cf.ac.uk/Dave/PERL/node36.html -- Discusses bitwise operators including bitwise shifts
Programming Perl -- General information about bitwise operations, probably the book you are already reading
CPAN also has some interesting/useful modules including Tie::VecArray and Bit::Vector.
There is a site with some really good information on this topic, I can't find it at the moment, I will update this node with the information when I find it though.
Hope this helps,
tigervamp
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: bitwise operators
by mattr (Curate) on May 29, 2001 at 15:42 UTC |