Coder Profile - Show off your skills, get a coder profile.
 
 
 
Please login to rate source codes.

Click here to register a free account with us.
Language Posted Comments
PHP (Hypertext Preprocessor) 2.52 Years Ago 9
Rating Downloadable Plain Text
0.00 - - - 5,336 Chrs
Total Ratings Views Screenshot
0 978 - - -

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.
 
VBAssassin     Posted 1.77 Year 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 1.77 Year 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 1.77 Year 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 1.77 Year 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 1.78 Year 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 1.78 Year 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 2.45 Years Ago
 
 
Ahh, alright good point.
 
VBAssassin     Posted 2.45 Years 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 2.45 Years 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
Scott Thompson (24)
United Kingdom, Lincolnshire
VBAssassin has 32 fans
become a fan
� Applications
Articles Articles (13)
Source Codes Source Codes (61)
� About Me
About Me About Me
User Groups User Groups (9)
Portfolio Portfolio (8)
Friends Friends (178)
� Misc
Overview Overview
Send A Friend Invite
Send Message Send A Message
RSS Whole Profile Feed
 
 
Latest News About Coder Profile
Coder Profile Poll
If you made money from keeping your profile up to date, say $30 and up, per month. What extra time would you spend on your profile?

No extra time
A few hours at weekends
A whole day each week
Every minute i can get free


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