> 2".int div2 = a / 8; //should be replaced with "a >> 3"."/>
首页 百科知识 使用移位操作来代替'

使用移位操作来代替'

时间:2022-09-22 百科知识 版权反馈
【摘要】:int div = a / 4; //should be replaced with "a >> 2".int div2 = a / 8; //should be replaced with "a >> 3".

"/"是一个很“昂贵”的操作,使用移位操作将会更快更有效。

 

例子:

public class SDIV {

   public static final int NUM = 16;

   public void calculate(int a) {

       int div = a / 4;            //should be replaced with "a >> 2".

       int div2 = a / 8;         //should be replaced with "a >> 3".

       int temp = a / 3;

    }

}

 

更正:

public class SDIV {

   public static final int NUM = 16;

   public void calculate(int a) {

       int div = a >> 2; 

       int div2 = a >> 3;

       int temp = a / 3;       // 不能转换成位移操作

    }

}


免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

我要反馈