posts - 41,  comments - 40,  trackbacks - 0

/*******************************************************************************
 *
 * 乘除法和等效的位运算
 *
 * 1024 = 2*2*2*2*2*2*2*2*2*2 = 100 0000 0000
 *
 ******************************************************************************/
class BitOperation
{
  public static void main( String[] args )
  {
    //被除数
    long dividend=182495073L;

    long temp=0L;

    long begin=0L;


    ////////////////////////////////////////////////////////////////////////////
    //
    // dividend/1024等效于dividend>>10
    //
    ////////////////////////////////////////////////////////////////////////////
    begin=System.currentTimeMillis();

    for( int i=0; i<10000000; i++ )
       {
         temp=dividend/1024;
       }

    System.out.println( temp+" 总共耗时:"+( System.currentTimeMillis()-begin )+"毫秒" );

    begin=System.currentTimeMillis();

    for( int i=0; i<10000000; i++ )
       {
         temp=dividend>>10;
       }

    System.out.println( temp+" 总共耗时:"+( System.currentTimeMillis()-begin )+"毫秒\r\n" );


    ////////////////////////////////////////////////////////////////////////////
    //
    // dividend*1024等效于dividend<<10
    //
    ////////////////////////////////////////////////////////////////////////////
    begin=System.currentTimeMillis();

    for( int i=0; i<10000000; i++ )
       {
         temp=dividend*1024;
       }

    System.out.println( temp+" 总共耗时:"+( System.currentTimeMillis()-begin )+"毫秒" );

    begin=System.currentTimeMillis();

    for( int i=0; i<10000000; i++ )
       {
         temp=dividend<<10;
       }

    System.out.println( temp+" 总共耗时:"+( System.currentTimeMillis()-begin )+"毫秒\r\n" );


    ////////////////////////////////////////////////////////////////////////////
    //
    // dividend%1024等效于dividend-( dividend>>10<<10 )
    //
    ////////////////////////////////////////////////////////////////////////////
    begin=System.currentTimeMillis();

    for( int i=0; i<10000000; i++ )
       {
         temp=dividend%1024;
       }

    System.out.println( temp+" 总共耗时:"+( System.currentTimeMillis()-begin )+"毫秒" );

    begin=System.currentTimeMillis();

    for( int i=0; i<10000000; i++ )
       {
         temp=dividend-( dividend>>10<<10 );
       }

    System.out.println( temp+" 总共耗时:"+( System.currentTimeMillis()-begin )+"毫秒\r\n" );
  }
}

posted on 2007-09-17 09:17 NeedJava 阅读(3099) 评论(3)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: