我在一个简单的计算器中使用toBinaryString将整数转换为它的二进制等价物,每当进行除加法运算之外的任何数学运算时,我都会得到前导1和0不应存在。

例如,我从10中减去5,应该得到5或101的二进制数,但是我得到


  输出:11111111111111111111111111110001000


这是我不知道的toBinaryString问题,还是我做错了什么?

主要代码:

    if (input == 1)
    {
        System.out.println("- ADDITION -");
        toDo = operator.getInputs(input); // Gets how many values to input.
        ArrayList<Integer> list = new ArrayList<>(toDo);
        operator.addToList(list, toDo); // Adds values input to list.
        operator.addBinary(list); // Add values in 'list' together.
    }

    if (input == 2)
    {
        System.out.println("- Subtraction -");
        toDo = operator.getInputs(input); // Gets how many values to input.
        ArrayList<Integer> list = new ArrayList<>(toDo);
        operator.addToList(list, toDo); // Adds values input to list.
        operator.subBinary(list); // Subtract values in 'list' for eachother.
    }

    if (input == 3)
    {
        System.out.println("- Multiplication -");
        toDo = operator.getInputs(input); // Gets how many values to input.
        ArrayList<Integer> list = new ArrayList<>(toDo);
        operator.addToList(list, toDo); // Adds values input to list.
        operator.multBinary(list); // Multiplies values in 'list' together.
    }


方法:

class binaryOperations
{
private Scanner scan = new Scanner(System.in);

int getInputs(int b)
{
    int input;
    System.out.println("How many integers do you wish to input: ");
    return input = scan.nextInt();
}

int printBinary(int b) // Number to convert
{
    String foo = Integer.toBinaryString(b); // Convert input to binary
    return (Integer.parseInt(foo));
}

ArrayList<Integer> addToList(ArrayList<Integer> list, int toDo)
{
    for (int i = 0; i < toDo; i++)
    {
        if (i == 0)
            System.out.println("Please enter the first integer:");
        else
            System.out.println("Please enter the next integer: ");

        int input = scan.nextInt();
        list.add(input);
    }
    return list;
}


// Addition //
void addBinary(ArrayList<Integer> list)
{
    int temp = 0, sum = 0;
    for (int i = 0; i <= list.size() - 1; i++)
    {
        temp = list.get(i);
        sum += temp;
    }

    String foo = Integer.toBinaryString(sum); // convert the sum to a string

    System.out.println("The sum of the numbers in binary is " + foo);
}

// Subtraction //
void subBinary(ArrayList<Integer> list)
{
    int temp = 0, difference = 0;
    for (int i = 0; i <= list.size() - 1; i++)
    {
        temp = list.get(i);
        difference -= temp;
    }

    String foo = Integer.toBinaryString(difference); // convert the difference to a string

    System.out.println("The difference of the numbers in binary is " + foo);
}

// Multiplication //
void multBinary(ArrayList<Integer> list)
{
    int temp = 0, products = 0;
    for (int i = 0; i <= list.size() - 1; i++)
    {
        temp = list.get(i);
        products *= temp;
    }

    String foo = Integer.toBinaryString(products);

    System.out.println("The products of the numbers in binary is " + foo);
}
}

最佳答案

您的subBinary运行不正常。它的作用是:

0 - firstElementofList - secondElementofList ...


因此,在您的情况下,如果您的列表包含{10,5},则将得到-15,该值由以下二进制值精确表示:

11111111 11111111 11111111 11110001


您需要做的是更改它,以便从第一个中减去其余的:

void subBinary(ArrayList<Integer> list)
{
    int temp = 0, difference = list.get(0);
    for (int i = 1; i <= list.size() - 1; i++)
    {
       temp = list.get(i);
       difference -= temp;
    }

    String foo = Integer.toBinaryString(difference); // convert the difference to a string

    System.out.println("The difference of the numbers in binary is " + foo);
}


现在,如果您提供{5,10},您将得到-5

10-06 14:03