# bitwise operators

23 replies to this topic

### #21 Zane

Zane
• 4,134 posts

Posted 31 October 2006 - 06:26 PM

oh ok..I get it

I can't exactly explain why I get it, but i reminds me of the same way
CHMOD permissions work..
but more extended

Want to thank me? Contribute to my PayPal piggy-bank

### #22 Barand

Barand
• Moderators
• Sen . ( ile || sei )
• 18,021 posts

Posted 31 October 2006 - 06:45 PM

oh ok..I get it

I can't exactly explain why I get it, but i reminds me of the same way
CHMOD permissions work..
but more extended

Yes, it's just the same
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

 |baaGrid| easy data tables - and more|baaChart| easy line, column and pie charts

### #23 roopurt18

roopurt18
• Staff Alumni
• 3,749 posts
• LocationCalifornia, southern

Posted 31 October 2006 - 06:55 PM

It is the way chmod works:

chmod gives you four columns, each of which can be a bit combination of the following:

4 : 0100
2 : 0010
1 : 0001

Since the highest bit possible is the (counting from the right) 3rd bit, we can ignore the 4th bit and drop it.

4 : 100
2 : 010
1 : 001

Now, chmod has 4 possible settings, each setting is a combination of those 3 bits.  So all total we need at least  4 * 3 = 12 bits to represent a chmod setting.

chmod columns:
```SUI       Owner        Group       Others
000       000           000          000
```

Counting Problems
Q: How many combinations exist for each chmod column?
A: 3 binary digits, each with 2 possible (0 or 1) values minus the number of impossible values (000): 2 * 2 * 2 - 1 = 7

Q: How many possible chmod permissions exist?
A: Our answer from above multiplied by the number of columns (4), or: 28
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

### #24 roopurt18

roopurt18
• Staff Alumni
• 3,749 posts
• LocationCalifornia, southern

Posted 31 October 2006 - 07:06 PM

An extension to my reply above.  When you're using bits to represent settings you're packing them into a variable.  That variable might be 16, 32, 64, etc. bits in length.  The size of that variable determines how many preferences or permissions you can pack in there.

For instance, a 32-bit variable can represent 32 permissions with on / off settings.

If you have a preference that is more than on / off, maybe it's: very low, low, avg, high, very high.  That's 5 settings for one permission / preference.  How many bits are necessary to store 5 unique combinations?  The answer is 3 bits:
001 - very low
010 - low
011 - avg
100 - high
101 - very high

Notice that we didn't use the values 000, 110, or 111.

Going back to our 32-bit number, if we were storing the previous preference / permission, 3 of the 32 bits would be taken by that preference.  This leaves us with 29 more bits to work with; we could store 29 more on / off preferences or maybe 23 more on / off preferences and another preference that requires 6 bits (23 + 6 = 29).

As for how we would go about storing and retrieving such a preference:
```<?php
define( 'PNAME_VLOW', 0x01 );
define( 'PNAME_LOW', 0x02 );
define( 'PNAME_AVG', 0x03 );
define( 'PNAME_HIGH', 0x04 );
define( 'PNAME_VHIGH', 0x05 );
define( 'PNAME_ALL', 0x07 );

\$prefs = 0x00; // Set initial preferences

// A lot of code that modifies the value in \$prefs

// Now determine which of PNAME the user has permission to do
switch( \$prefs & PNAME_ALL ){
case PNAME_VLOW:
// very low
break;
case PNAME_LOW:
// low
break;
case PNAME_AVG:
// average
break;
// And so on...
}
?>```

Notice now that instead of checking if the bit is turned on (Using \$prefs & PNAME_const), we're checking which out of all the bits for the preference are on and which value they're equal to.
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users