Jackal Posted November 18, 2010 Report Share Posted November 18, 2010 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. Quote Link to comment Share on other sites More sharing options...
bubu Posted November 18, 2010 Report Share Posted November 18, 2010 vai definējot konstantes jāuzmanās, lai konstantei piešķirtais skaitlis nepārsniedz 32 bitus?Jā, tieši tā. Quote Link to comment Share on other sites More sharing options...
Jackal Posted November 18, 2010 Author Report Share Posted November 18, 2010 Tad tas nozīmē, ka maksimālais ar ko es varu nodefinēt konstanti ir 0x80000000? Quote Link to comment Share on other sites More sharing options...
bubu Posted November 18, 2010 Report Share Posted November 18, 2010 Vai nu 0x80000000 vai 0x40000000, atkarībā no tā vai php integer'us apstrādā signed vai unsigned veidā (un ja signed, tad vai korekti rīkojās ar to 1<<31 skaitli). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.