Jump to content
php.lv forumi

bitmaskas


Jackal

Recommended Posts

Gribu taisīt lietotāju tiesības izmantojot bitmaskas. Piemēram, ir kods:

 

class permissions {
   const ADD_CONTENT = 0x1;
   const ADD_OWN_CONTENT = 0x2;
   const EDIT_CONTENT = 0x4;
   const EDIT_OWN_CONTENT = 0x8;
   const DELETE_CONTENT = 0x10;
   const DELETE_OWN_CONTENT = 0x20;
   const ADD_COMMENT = 0x40;
   const VIEW_COMMENT = 0x80;
}

$myPermissions = permissions::ADD_CONTENT | permissions::ADD_OWN_CONTENT | permissions::EDIT_CONTENT | permissions::EDIT_OWN_CONTENT | permissions::DELETE_CONTENT | permissions::DELETE_OWN_CONTENT | permissions::ADD_COMMENT;

       if(hasPermission(permissions::ADD_CONTENT, $myPermissions)){
           echo 'User can add contnet';
       } else {
           echo 'User cannot add content';
       }

function hasPermission($permissionToCheck, $userPermissions){
       return $permissionToCheck & $userPermissions;
   }

 

Nav skaidrs par & un | . Daudzās vietās esmu lasījis, ka izmantojot šos operātorus jāuzmanās, lai nepārsniegtu 32 bitus (atkarībā no procesora 32 vai 64). Nav skaidrs kurā vietā tur var rasties 32 biti? vai definējot konstantes jāuzmanās, lai konstantei piešķirtais skaitlis nepārsniedz 32 bitus? Varbūt stulbs jautājums, bet man par bitu operācijām vispār ir diezgan liela neskaidrība.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...