计算机整数乘积计算

来源:互联网 发布:桌面管理软件 知乎 编辑:程序博客网 时间:2024/05/22 03:23

计算机计算整数乘积的原理:
计算机计算乘积过程

/** * Caculate the multiplication between two integer * @author open201 * */public class Two {    /**     * Fundamental method     * f(n) = O(n^2)      * @param a     * @param b     * @return     */    public static int naiveMul(int a,int b){        int x = 0;        //判断a中出现1的位置,每当出现1就将b的移位运算结果加到最终的结果中。        while(a > 0){//n bits              if(a%2==1)                x = x + b; //n bits            a = a>>1;            b = b<<1;        }        return x;    }    public static void main(String [] args){        System.out.println(naiveMul(20,60));    }}
0 0
原创粉丝点击