【问题标题】:Java booth algorithm Shift or addition troubleJava 展位算法 移位或加法麻烦
【发布时间】:2021-12-14 02:02:14
【问题描述】:

我只需要一点帮助将亭算法编程到 java 中,我真的不知道如何解决某些问题,现在添加正确的移位使整个答案 1(将显示一些输入和输出示例来解释和show) 我知道它可以正确转换为二进制,所以我认为我的问题在于我的班次和添加功能。

public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the first number: ");
    int operand1 = sc.nextInt();
    System.out.print("Enter the second number: ");
    int operand2 = sc.nextInt();
    String answer = multiply(operand1, operand2);
    System.out.println(answer);
}

 static String appendZeros(int n){
        String result = "";
        for(int i = 0; i < n; i++) result += "0";
        return result;
    }

public static String toBinary(int x, int len)
{
    if (len > 0)
    {
        return String.format("%" + len + "s",
                        Integer.toBinaryString(x)).replaceAll(" ", "0");
    }

    return null;
}

static String add(String a, String b){
    String result = "";
    char carry = '0';
    for(int i = a.length()-1; i >= 0; i--){
        String condition = "" + a.charAt(i) + b.charAt(i) + carry;
        switch(condition){
            case "000": result = "0" + result; break;
            case "001": result = "1" + result; carry = '0'; break;
            case "010": result = "1" + result; break;
            case "011": result = "0" + result; break;
            case "100": result = "1" + result; break;
            case "101": result = "0" + result; break;
            case "110": result = "0" + result; carry = '1'; break;
            case "111": result = "1" + result; break;
        }
    }
    return result;
}

 static String rightShift(String str){
        String result = "";
        for(int i = 0; i < str.length(); i++){
            if(i == 0) result += str.charAt(i);
            else result += str.charAt(i-1);
        }
        return result;
 }

static String multiply(int a, int b){
    String op1 = toBinary(a, 8);
    String op2 = toBinary(b, 8);
    String negop2 = toBinary(-b, 8);
    if (op1.length() > 8) 
    {
        op1 = op1.substring(op1.length() - 8);
    } 
    if (op2.length() > 8) 
    {
        op2 = op2.substring(op2.length() - 8);
    } 
    if (negop2.length() > 8) 
    {
        negop2 = negop2.substring(negop2.length() - 8);
    } 
    System.out.println(op1 + " " + op2 + " " + negop2);
    char prev = '0';
    String product = appendZeros(16-op1.length())+op1;
    for(int i = 0; i < 8; i++){
        if(i > 0) prev = product.charAt(15);
        if(product.charAt(15)=='0' && prev == '1'){
            String temp = appendZeros(8-op2.length()) + op2 + appendZeros(8);
            product = add(product, temp);
        }
        if(product.charAt(15)=='1' && prev == '0'){
            String temp = appendZeros(8-negop2.length()) + negop2 + appendZeros(8);
            product = add(product, temp);
        }
        product=rightShift(product);
    }
    return product;
}

输入 9 1 输出 1111111111111111 预期 0000000000001001
输入 9 9 输出 1111111111110111 预期 0000000001010001

【问题讨论】:

  • 我只是想补充一下,我发现有太多的原因似乎是 shift 函数一直运行太多,我可能需要重写我的循环,因为它运行了 8 次谢谢怎么写的

标签: java binary


【解决方案1】:

我发现了我的问题,prev 变量被调用得太晚了,所以它没有正确更新函数,prev 值需要在班次发生之前更新。我在调用 shift 函数之前将它移到了右边,并删除了 if 语句并修复了它,现在它可以正常工作了。

for(int i = 0; i < 8; i++){
        //System.out.println("Did it change back?" + product.charAt(15) + prev);
        //System.out.println("i = " + i);
        if(product.charAt(15)=='0' && prev == '1'){
            String temp = appendZeros(8-op2.length()) + op2 + appendZeros(8);
            System.out.println(temp);
            product = add(product, temp);
            System.out.println("Add " + product);
        }
        if(product.charAt(15)=='1' && prev == '0'){
            String temp = appendZeros(8-negop2.length()) + negop2 + appendZeros(8);
            System.out.println(temp);
            product = add(product, temp);
            System.out.println("Subtract " + product);
        }
        prev = product.charAt(15);
        product=rightShift(product);
        System.out.println("Shift " + product);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    相关资源
    最近更新 更多