简体   繁体   English

使用整数数组的Java二进制乘法不起作用

[英]Java binary multiplication using integer arrays not working

I'm making a program that accepts two decimal numbers and convert them into binary numbers, which are stored in integer arrays. 我正在编写一个程序,该程序接受两个十进制数字并将它们转换为二进制数字,并存储在整数数组中。 Then I need to do multiplication using the two integer arrays. 然后,我需要使用两个整数数组进行乘法。 The result should also be a binary integer array (I need to validate that using a for loop). 结果还应该是一个二进制整数数组(我需要使用for循环进行验证)。 Then I convert them result to decimal number. 然后,我将它们的结果转换为十进制数。

So far, I have the following code. 到目前为止,我有以下代码。 My logic to convert the decimal number to binary works fine and vice verse. 我将十进制数字转换为二进制的逻辑工作正常,反之亦然。 However, the binary result is always somehow smaller than the expected result. 但是,二进制结果总是比预期结果小。 I have spent a lot of time on this, could you help me check what is wrong? 我在此上花了很多时间,您能帮我检查一下哪里出了问题吗?

public class BinaryMultiplication {
public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    int num1 = scanner.nextInt();
    int num2 = scanner.nextInt();

    int[] binaryNum1 = toBinary(num1);
    int[] binaryNum2 = toBinary(num2);
    System.out.println("Expected result: " + num1 * num2);
    System.out.println("Decimal number 1: " + toDecimal(binaryNum1));
    System.out.println("Decimal number 2: " + toDecimal(binaryNum2));

    int[] resultBinaries = new int[100];

    for (int i = 0; i < resultBinaries.length; ++i) {
        resultBinaries[i] = 0;
    }

    for (int i = 0; binaryNum1[i] != -1; ++i) {
        for (int j = 0; binaryNum2[j] != -1; ++j) {
            resultBinaries[i + j] += binaryNum1[i] * binaryNum2[j] % 2;
            resultBinaries[i + j] %= 2;
        }
    }
    resultBinaries[99] = -1;

    for (int i = 0; resultBinaries[i] != -1; ++i) {
        if (resultBinaries[i] > 1) {
            System.out.println("The result is not a binary!!");
        }
    }

    System.out.println("Actual decimal result: " + toDecimal(resultBinaries));
}

public static int toDecimal(int[] binaryNum) {
    int result = 0;
    int factor = 1;
    for (int i = 0; binaryNum[i] != -1; ++i) {
        result += binaryNum[i] * factor;
        factor *= 2;
    }
    return result;
}

public static int[] toBinary(int num) {
    int[] binaries = new int[100];

    int index = 0;

    while (num > 0) {
        binaries[index++] = num % 2;
        num /= 2;
    }

    binaries[index] = -1;

    return binaries;
}
}

A sample input & output: ( the binary validation loop works fine) 输入和输出示例:(二进制验证循环工作正常)

45 67
Expected result: 3015
Decimal number 1: 45
Decimal number 2: 67
Actual decimal result: 2871
   for (int i = 0; binaryNum1[i] != -1; ++i) {  
        for (int j = 0; binaryNum2[j] != -1; ++j) {  
            resultBinaries[i + j] += binaryNum1[i] * binaryNum2[j] % 2;  
            resultBinaries[i + j] %= 2;  
        }  
    }

What happens when resultBinaries[i + j] increases to 2? resultBinaries[i + j]增加到2时会发生什么? It's reduced to 0 and then resultBinaries[i + j + 1] should be increased with 1, but this isn't happening in the code as far as I can see. 它减小到0,然后resultBinaries[i + j + 1]应该增加1,但是就我所知,这在代码中没有发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM