OK, I am very curious about bits and bitwise operators, as I recently attempted to get a multi-line conditional assignment to work using |. Anyways, all kinds of unpredictable results. I finally gave up and just made an if / elseif sequence. Anyways, I don't care about that so much right now.
Originally I brought up this issue here. mtmosier referred me to Shift and Logical Operators (sun.com) and then I was also reading from MySQL Set Column which discusses this to a certain extent and then of course from php.net Bitwise Operators. The whole thing has me fascinated but I have limited grey matter so it's a "bit" of a struggle! 🙂
Here is an example of the use of the bitwise operators in mysql:
From Monte Ohrt (mysql user notes)
To add an element to an existing set, use this syntax:
update TABLE set COLUMN = COLUMN | NUM;
where TABLE is your table name, COLUMN is your
column of type SET(), and NUM is the decimal value
of the element you want to add, which would be one of
1,2,4,8,16,...
To add more than one element, use the sum.
So to add set elements 1, 2 and 8, use 11 as your
NUM value (1 + 2 + 8).
To remove an element, use this syntax:
update TABLE set COLUMN = COLUMN & ~NUM;
again, where NUM is one of 1,2,4,8,16,... or sum of
them to remove multiple elements.
I have all kinds of questions and anyone who would like to help out would be greatly appreciated.
First of all, using the mysql set column as an example, the user doc said that there can be a total of 64 different set values. However, I can only see that there would be 15. For instance, 'a' -> decimal 1 -> 0001, 'b' -> decimal 2 -> 0010, etc. If I enter decimal 15 that would be 1111, which appears to me to be the maximum?!?
OK, I understand now about & | ^ and even ~ as far as how it processes the bits. The sun.com page above really helped with that. And then they gave an example use of "managing sets of boolean flags", which sounds wonderfully practical. They gave the example of 4 flags: visible, draggable, selectable and editable. Then they used the bitwise operator on them using the decimal value of the corresponding flag. I guess this is in line with my last question. Does that mean that you are limited to only 4 flags corresponding to the on/off state of each bit in the four positions??
The easier to understand your explanations the better, if you have the time of course...