NYCPHP Meetup

NYPHP.org

[nycphp-talk] Bitwise Operators

Kamm, William R (Bill), ALABS wkamm at att.com
Wed Jun 29 10:04:57 EDT 2005


Years ago (back in the '80s), hardware was expensive.  Memory and disk
storage were at a premium.  When we wrote programs, we kept efficiency
in mind.  Using bits to set/clear flags, we could get 8 flags using only
one byte of storage.  Today it seems ridiculous to do things like that,
when a boolean variable will do, but at that time computers were slow
and expensive.  I don't see as much value to bit operations today as I
did in the past.

Bill

-----Original Message-----
From: talk-bounces at lists.nyphp.org [mailto:talk-bounces at lists.nyphp.org]
On Behalf Of Flavio daCosta
Sent: Wednesday, June 29, 2005 8:44 AM
To: NYPHP Talk
Subject: Re: [nycphp-talk] Bitwise Operators


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
_______________________________________________
New York PHP Talk Mailing List
AMP Technology
Supporting Apache, MySQL and PHP
http://lists.nyphp.org/mailman/listinfo/talk
http://www.nyphp.org



More information about the talk mailing list