That is true if you can tell at *compile time* that the divisor is a power of
2.  On the other hand if the divisor is an indeterminate that is constrained to
be a power of 2 then you have to do the shift on your own.

/* *********************** Example (Before) ************************** */
public int getRandomPowerOfTwo( Random random )
{
    // gets 5 bit int ( value between 0 and 31 )
    int power = random.next( 5 );

    // 2 raised to 31 is beyond the int range
    // so get another instead
    while( power == 31 )
    {
        power = random.next( 5 );
    }
    return 1 << power;
}

public int processValue( int value )
{
    Random random = new Random();

    // following division not converted to shift
    return value / getRandomPowerOfTwo( random );
}

/* *********************** Example (After) ************************** */
public int getRandomPowerOfTwoExponent( Random random )
{
    // gets 5 bit int ( value between 0 and 31 )
    int power = random.next( 5 );

    // 2 raised to 31 is beyond the int range
    // so get another instead
    while( power == 31 )
    {
        power = random.next( 5 );
    }
    return power;
}

public int processValue( int value )
{
    Random random = new Random();

    // following division performed by explicit shift
    return value >> getRandomPowerOfTwoExponent( random );
}

/* ****************************************************************** */

-- Roger Glover


--- Joseph Ottinger <[EMAIL PROTECTED]> wrote:
> Actually, nearly every modern compiler will optimize divisions by powers of 
> two *into* bit shifts, so >> isn't faster than / at all... unless your 
> compiler/optimizer is retarded.
> 
> 
> >From: "Paul Franz" <[EMAIL PROTECTED]>
> >Reply-To: "JDJList" <[EMAIL PROTECTED]>
> >To: "JDJList" <[EMAIL PROTECTED]>
> >Subject: [jdjlist] Re: Shift Operator: What is it good for?
> >Date: Thu, 5 Sep 2002 14:40:06 -0400
> >
> >Shift Operator: What is it good for?Dividing by a multiple of 2. This is 
> >faster than using the / operator.
> >
> >Paul Franz
> >   ----- Original Message -----
> >   From: Hansen, Knud
> >   To: JDJList
> >   Sent: Thursday, September 05, 2002 2:19 PM
> >   Subject: [jdjlist] Shift Operator: What is it good for?
> >
> >
> >   During the discussion about 'Getting digits from an integer' there was a 
> >reference to the binary shift operator >>.  In my 15 years of programming I 
> >have never once used the shift operator.  I am sure it must be used for 
> >something, but I can not think of anything useful.  Anybody out there every 
> >use it?  If so, what for?
> >
> >   Thanks
> >   Knud Hansen
> >
> >
> >
> >
> >
> >   To change your JDJList options, please visit: 
> >http://www.sys-con.com/java/list.cfm
> >
> >
> >To change your JDJList options, please visit: 
> >http://www.sys-con.com/java/list.cfm
> 
> 
> -----------------------------------------------
> Joseph B. Ottinger       [EMAIL PROTECTED]
> http://enigmastation.com          IT Consultant
> 
> _________________________________________________________________
> Join the world�s largest e-mail service with MSN Hotmail. 
> http://www.hotmail.com
> 
> 
> To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm


=====
-- Roger Glover

__________________________________________________
Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
http://finance.yahoo.com

To change your JDJList options, please visit: http://www.sys-con.com/java/list.cfm

Reply via email to