Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: What A Wonderful World: Bitmasks!

by BUU (Prior)
on Dec 08, 2002 at 08:10 UTC ( [id://218350]=note: print w/replies, xml ) Need Help??


in reply to What A Wonderful World: Bitmasks!

A slightly differen response. Would you mind giving a very simple explanation of how exactly bitmasks work? (Along with possibly what & and | do..)

Replies are listed 'Best First'.
Re: Re: What A Wonderful World: Bitmasks!
by tachyon (Chancellor) on Dec 08, 2002 at 17:44 UTC

    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.

      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.";
      
      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.

Re: Re: What A Wonderful World: Bitmasks!
by demerphq (Chancellor) on Dec 08, 2002 at 15:53 UTC
    A bitmaks and binary tests are just the idea of compressing a bunch of small width data (usually booleans, ie 1 bit values) into a byte. So for instance we might use a bit field to represent which external walls of a rectangle in a spreadsheet get colored in and which internal lines get colored in:
    Bit 7 : reserved 6 : reserved 5 : inside horiz 4 : inside vert 3 : bottom 2 : top 1 : right 0 : left
    So a value of 255 corresponds to all sides being colored in. A value of 3 means just the left and right, a value of 5 is the top and left etc...

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://218350]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-24 16:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found