I think that adequate explanation has been given to your question about how bitwise operations work, so I won't talk about that. You did ask though, how they could be useful, and there are many different areas in which bitwise operations are important, including cryptography, internal database management, graphical rendering engines, and algorithms. I do not want to bore/overwhelm you with complex examples, but here are a couple of general ideas behind thier use.

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


In reply to Re: bitwise operators by tigervamp
in thread bitwise operators by sir p freakly

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.