java实现加减乘除法

难易程度:★★★

重要性:★★★★★

阿里蚂蚁金服的面试中出现:要求手写实现“乘除法”

快手的面试中曾出现:使用位运算实现整数加法运算

public class BinaryOperation {

    public static void main(String[] args) throws Exception {
        System.out.println(binaryAdd(-6, -15));
        System.out.println(binaryMulti(5, 6));
        System.out.println(binaryMulti2(5, 6));
        System.out.println(binaryDiv(6, 3));
    }

    private static int binaryAdd(int a, int b) {// 正负数都包含在里面,不用分开处理
        int s = a ^ b;// 不考虑进位的和
        int jw = a & b;// 进位

        // 下面是 s + (jw<<1) 的计算
        while (jw != 0) {
            int jw_temp = s & (jw << 1);// 保存s + (jw<<1)的进位
            s = s ^ (jw << 1);// 保存s + (jw<<1)的和,不包含进位
            jw = jw_temp;// 赋值之后,还是计算s+(jw<<1),依旧是计算:进位以及不进位的和,当进位为0时,不进位的和就是最终的计算结果
        }
        return s;
    }

    private static int binaryMulti(int a, int b) {// 计算a*b
        if (a == 0 || b == 0)
            return 0;

        int res = 0;
        int base = a;
        while (b != 0) {
            if ((b & 1) != 0)
                res = binaryAdd(res, base);
            b >>= 1;
            base <<= 1;
        }

        return res;
    }
    private static int binaryMulti2(int a, int b) {// 计算a*b
        if (a == 0 || b == 0)
            return 0;
        
        if(b>a) {
            int tmp = a;
            a = b;
            b = tmp;
        }
        int res = 0;
        int shift = 0;
        while(b!=0) {
            if((b&1)!=0) {
                res += (a<<shift);
            }
            shift += 1;
            b >>= 1;
        }
        return res;
    }

    private static int binaryDiv(int a, int b) throws Exception {// 计算a/b
        if (b == 0)
            throw new Exception("分母不能为0");

        boolean flag = false;
        if ((a ^ b) < 0)
            flag = true;// 表示a,b异号;
        a = a >= 0 ? a : -a;
        b = b >= 0 ? b : -b;

        int res = 0;
        int aux = 0;//依次获取a的最高位
        int mask = 0x40_00_00_00;// 用来依次获取分母的最高位bit
        while (mask != 0) {
            aux = (aux << 1) | ((a & mask) == 0 ? 0 : 1);// 这里注意处理,尤其是后半部分表达式,很容易写成aux
                                                         // = (aux<<1) |
                                                         // (a&mask);

            if (aux >= b) {
                res = (res << 1) | 1;
                aux -= b;
            } else {
                res = (res << 1) | 0;
            }

            mask >>= 1;

        }

        return flag ? -res : res;
    }        
}

更多原创技术分享,请关注公众号“菜鸟名企梦

猜你喜欢

转载自blog.csdn.net/liewen_/article/details/82913837
今日推荐