我试图创建一个java模拟器,它将模拟4位数字的所有可能的有符号乘法。
我的代码布局使用字符来表示实际位。
我现在的问题是逐位拆分我的int输入(被乘数/乘数),将每个位放入一个新的char中,这样我就可以操作每个char(即位)来执行有符号乘法。
非常感谢任何指针
public class SignedMultiplication {
int multiplicand, multiplier, expected_product, actual_product;
char md_bit0, md_bit1, md_bit2, md_bit3, mr_bit0, mr_bit1, mr_bit2, mr_bit3;
char pr_bit0, pr_bit1, pr_bit2, pr_bit3, pr_bit4, pr_bit5, pr_bit6, pr_bit7;
for(multiplicand=-8;multiplicand<8;++multiplicand){
// set multiplicand bits for two's complement
md_bit0 = ;
md_bit1 = ;
md_bit2 = ;
md_bit3 = ;
for(multiplier = -8; multiplier < 8; ++multiplier){
// set multiplier bits for two's complement
mr_bit0 = ;
mr_bit1 = ;
mr_bit2 = ;
mr_bit3 = ;
// perform 4 bit multiplier operation
// result is in pr_bit0 through pr_bit7
// set actual product to value converting 2's complement to an integer
actual_product = ;
expected_product = multiplicand * multiplier;
if( expected_product != actual_product ){
System.out.println("Multiplicand: " + multiplicand + " Multiplier: " + multiplier +
" Expected Result: " + expected_product + " Obtained: " + actual_product );
}
}
}
}