OPT.SDIV
Use the shift operator on 'a / b' expressions
Description
This rule flags any `a/b' expression.
"/" is an expensive operation; using the shift operator is faster and more efficient.
Example
package OPT;
public class SDIV {
public static final int NUM = 16;
public void calculate(int a) {
int div = a / 4; // should be replaced by "a >> 2".
int div2 = a/ 8; // should be replaced by "a >> 3".
int temp = a / 3;
}
}
Repair
package OPT;
public class SDIV {
public static final int NUM = 16;
public void calculate(int a) {
int div = a >> 2;
int div2 = 8 >> 3;
int temp = a / 3; // not possible to replace this with shift
//operator.
}
}
|