toBinaryString领先1

时间:2016-04-19 18:40:38

标签: java binary

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

例如,我从10减去5,这应该给出5或101的二进制,但我得到

  

输出:11111111111111111111111111110001

这是我不知道的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);
}
}

1 个答案:

答案 0 :(得分:2)

您的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