java - 为 Booth 算法实现算术右移

标签 java string algorithm logic cpu-architecture

我试图使用 Java 实现 Booth 算法,但算术右移函数 (rightShift()) 在我的 multiply() 函数中被忽略了。是因为我为 product 变量使用了字符串吗?这是我的代码:-

import java.util.Scanner;
class BoothsAlgorithm{
    static String appendZeros(int n){
        String result = "";
        for(int i = 0; i < n; i++) result += "0";
        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 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 multiply(int a, int b){
        String op1 = Integer.toBinaryString(a);
        String op2 = Integer.toBinaryString(b);
        String negop2 = Integer.toBinaryString(-b);
        char prev = '0';
        String product = appendZeros(64-op1.length())+op1;
        for(int i = 0; i < 32; i++){
            if(i > 0) prev = product.charAt(63);
            if(product.charAt(63)=='0' && prev == '1'){
                String temp = appendZeros(32-op2.length()) + op2 + appendZeros(32);
                product = add(product, temp);
            }
            if(product.charAt(63)=='1' && prev == '0'){
                String temp = appendZeros(32-negop2.length()) + negop2 + appendZeros(32);
                product = add(product, temp);
            }
            rightShift(product);
        }
        return product.substring(32);
    }
    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();
        System.out.println("The multiplication is "+multiply(operand1, operand2));
    }
}

最佳答案

您需要 product = rightShift(product); 或类似的。 rightShift 返回一个包含其结果的新字符串。它不会,也不能更改调用方中 product 引用的字符串。

关于java - 为 Booth 算法实现算术右移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22240113/

相关文章:

algorithm - 需要 3D 旋转算法

java - 无法从操作监听器获取消息对话框

java - Maven 的 WAR 依赖

java - 使用java swing我在jtextarea中打开了一个文本文件,关闭它后JoptionPane对话框总是出现

c++ - 有没有办法只获取一行的一部分?

awk 中的字符串比较

c++ - 如何在 OpenCV 中过滤由重叠圆圈构成的轮廓

python - 算法 - 最小化 boolean 表达式

java - Bjacc/j if 和 while 定义

c - 每个 str-function 缩写词/首字母缩略词是什么意思?