Author Topic: bitwise operators  (Read 19059 times)

0 Members and 2 Guests are viewing this topic.

Offline ZaneTopic starter

  • Global Moderator
  • Fanatic
  • *
  • Posts: 4,022
  • Gender: Male
    • View Profile
Re: bitwise operators
« Reply #15 on: October 30, 2006, 07:35:21 PM »
So is that true for all numbers
17 through 31

here I am at another interesting point.
say I wanted to test such a question
with a loop


for($i=17;$i<32;$i++) {
  if(
decbin(31) | decbin($i))
     echo 
"$i is all of them";
}

would that work?

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

Offline Barand

  • Sen . (ile || sei)
  • Staff Alumni
  • 'Mind Boggling!'
  • *
  • Posts: 15,222
  • Gender: Male
  • php 5.3 MySql 5.1
    • View Profile
Re: bitwise operators
« Reply #16 on: October 30, 2006, 07:44:38 PM »
if it was your intention to print
Code: [Select]
17 is all of them
18 is all of them
19 is all of them
20 is all of them
21 is all of them
22 is all of them
23 is all of them
24 is all of them
25 is all of them
26 is all of them
27 is all of them
28 is all of them
29 is all of them
30 is all of them
31 is all of them
then it works ;)
|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

Offline ZaneTopic starter

  • Global Moderator
  • Fanatic
  • *
  • Posts: 4,022
  • Gender: Male
    • View Profile
Re: bitwise operators
« Reply #17 on: October 30, 2006, 07:52:22 PM »
eh..I was just wondering since the numbers 17 through 31
are both greater than 16 and NOT a power of two
that they would all be classified as
ALL of these
----------------------
1    E_ERROR
2    E_WARNING
4    E_PARSE
8    E_NOTICE
16   E_CORE_ERROR

I'm assuming they are


I just wrote the loop wrong...
that IF will always return true regardless I just realized...unless the result is 0



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

Offline Barand

  • Sen . (ile || sei)
  • Staff Alumni
  • 'Mind Boggling!'
  • *
  • Posts: 15,222
  • Gender: Male
  • php 5.3 MySql 5.1
    • View Profile
Re: bitwise operators
« Reply #18 on: October 30, 2006, 07:56:43 PM »
try
Code: [Select]
<?php
$ev 
= array(
    
1  =>  'E_ERROR'
    
2  =>  'E_WARNING'
    
4  =>  'E_PARSE'
    
8  =>  'E_NOTICE'
    
16 =>  'E_CORE_ERROR'
);

echo 
'<pre>';
for(
$i=17;$i<32;$i++) {
    echo 
"<br>$i = ";
    foreach (
$ev as $k=>$v) {
        if (
$i $k) echo "$v($k), ";                          // edited to show bit value
    
}
}
echo 
'</pre>';
?>


EDIT by zanus
Edit by Barand to show results with bit values

returns
Quote

17 = E_ERROR(1), E_CORE_ERROR(16)
18 = E_WARNING(2), E_CORE_ERROR(16)
19 = E_ERROR(1), E_WARNING(2), E_CORE_ERROR(16)
20 = E_PARSE(4), E_CORE_ERROR(16)
21 = E_ERROR(1), E_PARSE(4), E_CORE_ERROR(16)
22 = E_WARNING(2), E_PARSE(4), E_CORE_ERROR(16)
23 = E_ERROR(1), E_WARNING(2), E_PARSE(4), E_CORE_ERROR(16)
24 = E_NOTICE(8), E_CORE_ERROR(16)
25 = E_ERROR(1), E_NOTICE(8), E_CORE_ERROR(16)
26 = E_WARNING(2), E_NOTICE(8), E_CORE_ERROR(16)
27 = E_ERROR(1), E_WARNING(2), E_NOTICE(8), E_CORE_ERROR(16)
28 = E_PARSE(4), E_NOTICE(8), E_CORE_ERROR(16)
29 = E_ERROR(1), E_PARSE(4), E_NOTICE(8), E_CORE_ERROR(16)
30 = E_WARNING(2), E_PARSE(4), E_NOTICE(8), E_CORE_ERROR(16)
31 = E_ERROR(1), E_WARNING(2), E_PARSE(4), E_NOTICE(8), E_CORE_ERROR(16),
« Last Edit: October 30, 2006, 08:16:34 PM by Barand »
|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

Offline ZaneTopic starter

  • Global Moderator
  • Fanatic
  • *
  • Posts: 4,022
  • Gender: Male
    • View Profile
Re: bitwise operators
« Reply #19 on: October 30, 2006, 08:05:35 PM »
I'll have to study the pattern of that tomrrow...

Thanks a lot Barrand, I learned a lot in the last...while.

@roopurt too

I'm definitely putting this thread in the Repository when it's all said and done.
Some very useful information here.
« Last Edit: October 30, 2006, 08:08:02 PM by zanus »

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

Offline ZaneTopic starter

  • Global Moderator
  • Fanatic
  • *
  • Posts: 4,022
  • Gender: Male
    • View Profile
Re: bitwise operators
« Reply #20 on: October 31, 2006, 01:26:14 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

Offline Barand

  • Sen . (ile || sei)
  • Staff Alumni
  • 'Mind Boggling!'
  • *
  • Posts: 15,222
  • Gender: Male
  • php 5.3 MySql 5.1
    • View Profile
Re: bitwise operators
« Reply #21 on: October 31, 2006, 01:45:07 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
|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

Offline roopurt18

  • Guru
  • Fanatic
  • *
  • Posts: 3,658
  • Gender: Male
  • le sigh
    • View Profile
    • rbredlau
Re: bitwise operators
« Reply #22 on: October 31, 2006, 01:55:08 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:
Code: [Select]
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

Offline roopurt18

  • Guru
  • Fanatic
  • *
  • Posts: 3,658
  • Gender: Male
  • le sigh
    • View Profile
    • rbredlau
Re: bitwise operators
« Reply #23 on: October 31, 2006, 02:06:56 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:
Code: [Select]
<?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.
« Last Edit: October 31, 2006, 02:10:31 PM by roopurt18 »