首页 百科知识 使用移位操作代替'

使用移位操作代替'

时间:2022-09-22 百科知识 版权反馈
【摘要】:[i]但我个人认为,除非是在一个非常大的循环内,性能非常重要,而且你很清楚你自己在做什么,方可使用这种方法。否则提高性能所带来的程序晚读性的降低将是不合算的。int mul = a * 4; // should be replaced with "a<< 2".int mul2 = 8 * a; //should be replaced with "a << 3".

同上。

[i]但我个人认为,除非是在一个非常大的循环内,性能非常重要,而且你很清楚你自己在做什么,方可使用这种方法。否则提高性能所带来的程序晚读性的降低将是不合算的。

 

例子:

public class SMUL {

   public void calculate(int a) {

        int mul = a * 4;            // should be replaced with "a<< 2".

       int mul2 = 8 * a;         //should be replaced with "a << 3".

       int temp = a * 3;

    }

}

 

更正:

package OPT;

public class SMUL {

   public void calculate(int a) {

       int mul = a << 2; 

       int mul2 = a << 3;

       int temp = a * 3;       // 不能转换

    }

}


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

我要反馈