[nycphp-talk] Bitwise Operators
Flavio daCosta
nyphp at n0p.net
Wed Jun 29 08:44:27 EDT 2005
On 06/29/2005 02:18 AM, Daniel Krook wrote:
> So, to pose a new question on the value of learning about bitwise
> operators, what would you use them for in a standard web application? ACL
> determination? What else?
I utilize bit functions for flag handling and simple ACL in the same
fashion as the built error reporting
<http://www.php.net/manual/en/function.error-reporting.php>
for example:
define( 'D_VIEW_FORTUNE', ( 1 << 0 ) ); // 00000001
define( 'D_VIEW_LAST_LOGIN', ( 1 << 1 ) ); // 00000010
define( 'D_VIEW_LOGIN_COUNT', ( 1 << 2 ) ); // 00000100
...
// Normally dynamically set for each user
$display_view = D_VIEW_FORTUNE | D_VIEW_LOGIN_COUNT; // 00000101
...
if( $display_view & D_VIEW_FORTUNE )
{
// display fortune
}
...
I will also use them as an efficient way to detect every other row ;)
$i = 0;
while( ... )
{
if( ++$i&1 ) // Row is odd;
}
Flavio
More information about the talk
mailing list