|
Bitwise Settings (Class) - Updated 28th Feb 2010
Description
Specify an array of settings, and depending on the combination of settings will return a unique integer. This integer can then be used to workout the settings that were selected to produce that integer. For example: 5 = show username, show age... all that needs to be saved is the number 5, and that is enough to restore the users combination of settings.
A perfect example of where to use this is when selecting the mailing lists you are interested in... or maybe when you register, for selecting if to show your real name etc...
Technical
Uses the binary system and bitwise operators.
Allows a maximum of 30 settings per object, and ANY combination of those 30 settings.
New method added in the update: get_all_combinations_of($specific_setting_value)
Source Code
|
|
More PHP (Hypertext Preprocessor) Source Codes By VBAssassin
|
|
Please login to post comments. |
|
Yeah, anything first on understanding the binary numbering system... then on bitwise operations... it's like anything mate... it's only complicated if you don't properly understand it ;-)
A simple for loop looks complicated when you first start programming... brings back some memories lol.
Kind regards,
Scott
|
|
Aha I see. Still complicated though. I'll need to read through some stuff. Any good tutorials I should be looking at?
Thanks for taking the time to explain it.
|
|
It's nothing like that. You have to go deeper than integers... and look at the bits, for example:
4 & 5 = 4
Why? Look at the bits:
0100 & 0101 = 0100
Only the first 1 on the left matches in both numbers... so that 1 is left alone... when a 1 corresponds with a 0 it is replaced with a 0...
It took me a while to understand this... it was only when i kept coming back to it and "re learning" it through the years that finally it starts to get embedded in your head ;-)
Kind regards,
Scott
|
|
Ok, I can't seem to wrap my head around this. So if i understand this correctly the & is checking if the bits are true. Meaning its like doing if isset(setting_value) && isset(value) for some reason I think i'm totally missing the idea of this bitwise system.
|
|
Yeah, it's not the logical &&, it's the bitwise single &... which tests that both bits are true (1)... if both are true... then it returns that bit in the result... otherwise it returns false (0).
|
|
Great script. Just getting confused on this line:
[code] if ($setting_value & $value) { [/code]
I understand that setting_value is 15 (going by first example), and value is equal to 1 (lets say on first loop). How does it figure out that its enabled or not? Does the ampersand have a special cause in this line?
I could be missing something though, maybe I should grab some lunch and take another look.
Thanks
|
|
Because if it wen't up like you suggested, it would only allow 1 setting to be enabled.
Here is how it works eg:
0001 = 1st setting
0010 = 2nd setting
0100 = 3rd setting
etc
If you went up +1 each time... then you would get (with my code):
0001 = 1st setting on
0010 = 2nd setting on
0011 = 1st AND 2nd setting on
With your code...
0001 = 1st setting on
0010 = 2nd setting on
0011 = 3rd setting on
Now how would you set both the 1st and 3rd setting on, without hard coding it like 0100 = 1st and 2nd? Which simply would not work.
Doing it the way i posted is what then allows any combination of settings... it's only converted to decimal so it can easily be saved in a database etc.
Think of them as "switches" instead of a numbering system, amd each switch is responsible for a setting ;-)
Kind regards,
Scott
|
|
Pretty simple and effective script.
Why are you going up exponentially in the binary system. Why not just go up by one each time?
(0001, 0010, 0011, 0100, etc..)
|
|
 |
Scott Thompson (24) United Kingdom, Lincolnshire |
|
VBAssassin has 32 fans
become a fan |
|
 |
|
 |
|