Infinitylsx Infinitylsx - 7 months ago 13
Java Question

toBinaryString has leading 1's

I'm using toBinaryString in a simple calculator to convert an integer into its binary equivalent, and whenever doing any mathematical operation besides addition I get leading 1's, and 0's that shouldn't be there.

For example, I subtract five from 10 which should give 5 or 101 in binary, but instead I get


Output: 11111111111111111111111111110001


Is this an issue with toBinaryString that I don't know about, or am I doing something wrong?

Main Code:

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.
}


Methods:

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);
}
}

Answer

Your subBinary is not working well. What it does is:

0 - firstElementofList - secondElementofList ...

so in your case if your list contains {10,5} you will get -15 which is exactly represented by following binary value:

11111111 11111111 11111111 11110001

What you need to do is to change it probably to subtract the rest from the first one:

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);
}

Now if you supply {5,10} you will get -5