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....
| [reply] [d/l] |

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.
| [reply] [d/l] |

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....
| [reply] |

| [reply] |

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.
| [reply] |

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.";
| [reply] [d/l] [select] |