Coder Profile - Show off your skills, get a coder profile.
 
 
 
The Author
VBAssassin
Scott Thompson
Send A Message
Rating
Not
Rated
Please login to rate source codes.

Click here to register a free account with us.
General Details
PHP (Hypertext Preprocess...
Posted 307 Days Ago
297 Views
Received 0 Ratings
More Codes By This Author
Pick A Random Array Eleme...
Bitwise Settings (Class)...
Compare Ratings, and Retu...
Settings Class
Dynamic Parameters (Using...
Articles By This Author
[PHP] - Lets kill the com...
How to use Memcache on Wi...
PHP Sessions = Member Dat...
Speed Up Delivery of your...
AJAX - When to use it?

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
Comments
Please login to post comments.
 
VBAssassin     Posted 34 Days Ago
 
 
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
 
Dice     Posted 34 Days Ago
 
 
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.
 
VBAssassin     Posted 34 Days Ago
 
 
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
 
Dice     Posted 35 Days Ago
 
 
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.
 
VBAssassin     Posted 35 Days Ago
 
 
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).
 
Dice     Posted 36 Days Ago
 
 
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
 
Izzmo     Posted 282 Days Ago
 
 
Ahh, alright good point.
 
VBAssassin     Posted 282 Days Ago
 
 
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
 
Izzmo     Posted 283 Days Ago
 
 
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..)
Page 1 of 1
More "PHP (Hypertext Preprocessor)" Source Codes By This Author
Recently Posted "PHP (Hypertext Preprocessor)" Source Codes
Recently Rated "PHP (Hypertext Preprocessor)" Source Codes
 
 
Latest News About Coder Profile
Coder Profile Poll
Why do you get bored with programming?

Not enough time to do something productive
I run out of ideas
Too hard to show people my creations
Everything i do has too many errors, and it's too hard
I don't get bored!!!


please login to cast your vote
and see the results of this poll
Latest Coder Profile Changes
Coder Profile was last updated
1.28 Year Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright © 2007 - 2010, Scott Thompson, All Rights Reserved