【Java】使用位运算实现加减乘除

Solution 功能类


/**
 * 只使用位运算符实现两个整数的加减乘除功能
 */
class Solution {

    public static final int F = 1 << 31; // 符号位
    public static final int H = 1 << 30; // 高位

    // 加法
    public static int add(int a, int b) {
        if (b == 0) {
            return a;
        }
        int sum = a ^ b; // 异或求和
        int carry = (a & b) << 1; // 进位
        return add(sum, carry);
    }

    // 减法
    public static int sub(int a, int b) {
        b = add(~b, 1); // 先反码,再补码
        return add(a, b);
    }

    // 绝对值
    private static int abs(int n) {
        if (n == F) { // 最小负数溢出
            throw new RuntimeException("Numerical overflow");
        }
        if ((n & F) == F) {
            n = sub(0, n);
        }
        return n;
    }

    // 大于等于
    private static boolean gte(int a, int b) {
        if (((a & F) ^ (b & F)) == F) {
            return (a & F) == 0;
        }
        int m = H;
        while (m != 0) {
            if ((a & m) != (b & m)) {
                return (a & m) == m;
            }
            m = m >> 1;
        }
        return true;
    }

    // 乘法
    public static int mul(int a, int b) {
        boolean isMinus = ((a & F) ^ (b & F)) == F;
        a = abs(a);
        b = abs(b);
        int c = 0; // 结果
        while (b != 0) {
            if ((b & 1) == 1) {
                c = add(a, c);
            }
            a = a << 1;
            b = b >> 1;
        }
        if (isMinus) {
            c = sub(0, c);
        }
        return c;
    }

    // 除法
    public static int div(int a, int b) {
        if (b == 0) { // 除数不能为零
            throw new RuntimeException("Divide by zero");
        }
        boolean isMinus = ((a & F) ^ (b & F)) == F;
        a = abs(a);
        b = abs(b);
        int y = 0; // 余数
        int c = 0; // 结果
        int m = H; // 掩码
        while (m != 0) {
            y = y << 1;
            c = c << 1;
            if ((a & m) == m) {
                y = y | 1;
            }
            if (y != 0) {
                if (gte(y, b)) {
                    y = sub(y, b);
                    c = c | 1;
                }
            }
            m = m >> 1;
        }
        if (isMinus) {
            c = sub(0, c);
        }
        return c;
    }

}

TestDemo 测试类

public class TestDemo {

    public static void main(String[] args) {
        long time1 = System.currentTimeMillis();
        for (int i = 0; i < 1000_0000; i++) {
            int a = randomInt(10000);
            int b = randomInt(1000);
            if (Solution.add(a, b) != a + b) {
                System.out.printf("%s + %s = %s (%s)%n", a, b, Solution.add(a, b), a + b);
            }
            if (Solution.sub(a, b) != a - b) {
                System.out.printf("%s - %s = %s (%s)%n", a, b, Solution.sub(a, b), a - b);
            }
            if (Solution.mul(a, b) != a * b) {
                System.out.printf("%s * %s = %s (%s)%n", a, b, Solution.mul(a, b), a * b);
            }
            if (b != 0 && Solution.div(a, b) != a / b) {
                System.out.printf("%s / %s = %s (%s)%n", a, b, Solution.div(a, b), a / b);
            }
        }
        long time2 = System.currentTimeMillis();
        System.out.printf("Running time %.2f seconds", (time2 - time1) / 1000.0);
    }

    // 获取一个随机整数
    private static int randomInt(int m) {
        int n = (int) (Math.random() * m);
        if (Math.random() < 0.3) {
            n = -n;
        }
        return n;
    }

    // 展示32位比特信息
    public static String getBit(int n) {
        StringBuilder sb = new StringBuilder();
        for (int i = 31; i >= 0; i--) {
            sb.append(Math.abs((n & (1 << i)) >> i));
        }
        return sb.toString();
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值