Jump to content

bitwise help please


travis

Recommended Posts

alright,

basically I have a flag in my script that can have any or all of about 2000 different conditions.

ex.

$flag can contain any numbers from 1-2000 or all of 1-2000 different number conditions.

 

i used to code alot for amxx which was basically small coding and you could easily use a bitsum variable to get this done without messy arrays.

ex.

status_flags |= (1 << 15); // add condition 15
status_flags |= (1 << 100); // add condition 100
status_flags |= (1 << 1051); // add condition 1051
for (i = 0; i < 2000; i++)
if (status_flags & (1 << i)) // check for condition #
	print i;

and this code wound print 15 100 1051

 

i know all these operators are in php aswell but when i try the same principal in php it doesnt work the same.

$status_flags |= (1 << 15); // add condition 15
$status_flags |= (1 << 100); // add condition 100
$status_flags |= (1 << 1051); // add condition 1051
for ($i = 0; $i < 2000; $i++)
if ($status_flags & (1 << $i)) // check for condition #
	print $i . "<br>";

the if statement does work on 15 100 1051 but it also work on about 100 other numbers too.

ill show the first few for the ex

4
15
27
36
47
59
68
79
91
100
111
123
132
143
155
164
175
187
196
207
219
228
239
251
260
271
283
292
303
315
324
335
347
356
367
379
388
399
411
420
431
443
452

 

so my question is that if the bit var isnt going to work what are my other options to achieve this?

but I would perfer the bit

any and all help is welcome and apprieated

 

thank you

Link to comment
https://forums.phpfreaks.com/topic/190555-bitwise-help-please/
Share on other sites

Here is some code I used for user permissions many years ago where I stored in integers but needed to get the individual rights.

function CheckRights($right,$opid,$force=0) // Notifications
{
if(!isset($_SESSION['oprights']) || $force)
	$oplevel=GetSubadminRights($opid);
else
	$oplevel=$_SESSION['oprights'];
    $rgtstr="";
    for($i=0;$i<30;$i++)
    {
    	if((pow(2,$i) & $oplevel) == pow(2,$i))
        {
        	$rgtstr=$rgtstr . "1";
        }
        else
        {
        	$rgtstr=$rgtstr . "0";
        }
}
    //echo($rgtstr);
    for($i=0;$i<30;$i++) 
    {
    	$chkarr[]=substr($rgtstr,$i,1);
    }
//return 1;
    return($chkarr[$right]);
}
function GetSubadminPRights($opid)   // Permissions
{
global $link;

    $query=sprintf("select permissions,masteradmin from turnkeys_admin where uid=%d",$opid);
    $opResult=mysql_query($query,$link) or die(mysql_error()."--".$query);
    $opRow=mysql_fetch_row($opResult);
    $opLevel=$opRow[0];
// $masteradmin = $oprow[1];
// if($masteradmin) $opLevel = 134217726;  // set all permissions for master admin - disregard settings
    // Save in session
if($opid == $_SESSION['user_id'])
	$_SESSION['opprights'] = $opLevel;
    
    return($opLevel);
}

 

Hope it helps

Link to comment
https://forums.phpfreaks.com/topic/190555-bitwise-help-please/#findComment-1005055
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.