Java 为Booth'实现算术右移;s算法
我试图用Java实现Booth的算法,但在我的Java 为Booth'实现算术右移;s算法,java,string,algorithm,logic,computer-architecture,Java,String,Algorithm,Logic,Computer Architecture,我试图用Java实现Booth的算法,但在我的multiply()函数中忽略了算术右移函数(rightShift())。是因为我使用了一个字符串作为product变量吗?这是我的密码:- import java.util.Scanner; class BoothsAlgorithm{ static String appendZeros(int n){ String result = ""; for(int i = 0; i < n; i++) res
multiply()
函数中忽略了算术右移函数(rightShift()
)。是因为我使用了一个字符串作为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));
}
}
import java.util.Scanner;
类引导算法{
静态字符串追加零(int n){
字符串结果=”;
对于(int i=0;i=0;i--){
字符串条件=”“+a.charAt(i)+b.charAt(i)+进位;
开关(条件){
案例“000”:结果=“0”+结果;中断;
案例“001”:result=“1”+result;进位='0';中断;
案例“010”:结果=“1”+结果;中断;
案例“011”:结果=“0”+结果;中断;
案例“100”:结果=“1”+结果;中断;
案例“101”:结果=“0”+结果;中断;
案例“110”:result=“0”+result;进位='1';中断;
案例“111”:result=“1”+结果;中断;
}
}
返回结果;
}
静态字符串乘法(整数a、整数b){
字符串op1=Integer.toBinaryString(a);
字符串op2=Integer.toBinaryString(b);
字符串negop2=Integer.toBinaryString(-b);
char prev='0';
字符串乘积=追加零(64-op1.length())+op1;
对于(int i=0;i<32;i++){
如果(i>0)prev=product.charAt(63);
if(product.charAt(63)='0'和&prev=='1'){
字符串temp=appendZeros(32-op2.length())+op2+appendZeros(32);
产品=添加(产品,温度);
}
if(product.charAt(63)='1'和&prev=='0'){
字符串temp=appendZeros(32-negop2.length())+negop2+appendZeros(32);
产品=添加(产品,温度);
}
右移(产品);
}
返回产品。子字符串(32);
}
公共静态void main(字符串参数[]){
扫描仪sc=新的扫描仪(System.in);
System.out.print(“输入第一个数字:”);
int操作数1=sc.nextInt();
系统输出打印(“输入第二个数字:”);
int操作数2=sc.nextInt();
System.out.println(“乘法是”+乘法(操作数1,操作数2));
}
}
您需要product=rightShift(产品)代码>或类似。rightShift返回包含其结果的新字符串。它不会也不能更改调用者中product
引用的字符串