数学问题——位运算实现加减乘除

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wdays83892469/article/details/79773444

位运算实现加减乘除

位运算实现加法

这里写图片描述
这里写图片描述

    public static int add(int a, int b) {
        int sum = a;
        while (b != 0) {//不断的相加,直到没有进位信息
            sum = a ^ b;//产生进位信息 二进制后直接&运算就是(1&1=1 else 0)
            b = (a & b) << 1;
            a = sum;
        }
        return sum;
    }

位运算实现减法

    public static int negNum(int n) {//求相反数就是取反+1。不能用算数运算就用我们实现的add方法完成
        return add(~n, 1);
    }

    public static int minus(int a, int b) {//减法就是a+b的相反数
        return add(a, negNum(b));
    }

位运算实现乘法

这里写图片描述

public static int multi(int a, int b) {
        int res = 0;
        while (b != 0) {
            if ((b & 1) != 0) {
                res = add(res, a);//b的每一位(n)乘以a的左移n位 过程和普通乘法一样
            }
            a <<= 1;
            b >>>= 1;//无符号右移
        }
        return res;
    }

位运算实现除法

这里写图片描述

    public static boolean isNeg(int n) {
        return n < 0;
    }

    public static int div(int a, int b) {//除法,先以无符号算结果在把正负号加上
        int x = isNeg(a) ? negNum(a) : a;
        int y = isNeg(b) ? negNum(b) : b;
        int res = 0;
        for (int i = 31; i > -1; i = minus(i, 1)) {
            if ((x >> i) >= y) {
                res |= (1 << i);
                x = minus(x, y << i);
            }
        }
        return isNeg(a) ^ isNeg(b) ? negNum(res) : res;
    }

    public static int divide(int a, int b) {
        if (b == 0) {
            throw new RuntimeException("divisor is 0");
        }
        if (a == Integer.MIN_VALUE && b == Integer.MIN_VALUE) {
            return 1;
        } else if (b == Integer.MIN_VALUE) {
            return 0;
        } else if (a == Integer.MIN_VALUE) {
            int res = div(add(a, 1), b);
            return add(res, div(minus(a, multi(res, b)), b));
        } else {
            return div(a, b);
        }
    }

完全版本

public class AddMinusMultiDivideByBit {

     public static int add(int a, int b) {
        int sum = a;
        while (b != 0) {//不断的相加,直到没有进位信息
            sum = a ^ b;//产生进位信息 二进制后直接&运算就是(1&1=1 else 0)
            b = (a & b) << 1;
            a = sum;
        }
        return sum;
    }

    public static int negNum(int n) {//求相反数就是取反+1。不能用算数运算就用我们实现的add方法完成
        return add(~n, 1);
    }

    public static int minus(int a, int b) {//减法就是a+b的相反数
        return add(a, negNum(b));
    }

    public static int multi(int a, int b) {
        int res = 0;
        while (b != 0) {
            if ((b & 1) != 0) {
                res = add(res, a);//b的每一位(n)乘以a的左移n位 过程和普通乘法一样
            }
            a <<= 1;
            b >>>= 1;//无符号右移
        }
        return res;
    }

    public static boolean isNeg(int n) {
        return n < 0;
    }

    public static int div(int a, int b) {//除法,先以无符号算结果在把正负号加上
        int x = isNeg(a) ? negNum(a) : a;
        int y = isNeg(b) ? negNum(b) : b;
        int res = 0;
        for (int i = 31; i > -1; i = minus(i, 1)) {
            if ((x >> i) >= y) {
                res |= (1 << i);
                x = minus(x, y << i);
            }
        }
        return isNeg(a) ^ isNeg(b) ? negNum(res) : res;
    }

    public static int divide(int a, int b) {
        if (b == 0) {
            throw new RuntimeException("divisor is 0");
        }
        if (a == Integer.MIN_VALUE && b == Integer.MIN_VALUE) {
            return 1;
        } else if (b == Integer.MIN_VALUE) {
            return 0;
        } else if (a == Integer.MIN_VALUE) {
            int res = div(add(a, 1), b);
            return add(res, div(minus(a, multi(res, b)), b));
        } else {
            return div(a, b);
        }
    }

    public static void main(String[] args) {
        int a = (int) (Math.random() * 100000) - 50000;
        int b = (int) (Math.random() * 100000) - 50000;
        System.out.println("a = " + a + ", b = " + b);
        System.out.println(add(a, b));
        System.out.println(a + b);
        System.out.println("=========");
        System.out.println(minus(a, b));
        System.out.println(a - b);
        System.out.println("=========");
        System.out.println(multi(a, b));
        System.out.println(a * b);
        System.out.println("=========");
        System.out.println(divide(a, b));
        System.out.println(a / b);
        System.out.println("=========");

        a = Integer.MIN_VALUE;
        b = 32;
        System.out.println(divide(a, b));
        System.out.println(a / b);

    }

}

猜你喜欢

转载自blog.csdn.net/wdays83892469/article/details/79773444