Ich benutze toBinaryString in einem einfachen Rechner, um eine ganze Zahl in ihre binäre Entsprechung zu konvertieren, und wenn ich irgendeine mathematische Operation neben Addition mache, bekomme ich führende 1's und 0's, die nicht da sein sollten.toBinaryString hat führende 1's
Zum Beispiel ziehe ich fünf von 10 auf 5 oder 101 in binärer geben sollte, sondern ich
Ausgang: 11111111111111111111111111110001
Ist das ein Problem mit toBinaryString, die ich don‘ Ich weiß nichts, oder mache ich etwas falsch?
Haupt 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.
}
Methoden:
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);
}
}
Warum sollten sie nicht da sein? Sie haben solch einen gewundenen Code, nur um ein Problem zu zeigen. Bitte geben Sie [MCVE] an und erklären Sie, was Sie für falsch halten. – Savior
@Pillar gut, wenn der binäre Wert von 1 ist 00000001 Warum ist das, wenn Subtraktion mache Ich bekomme 'Der Unterschied der Zahlen in binären ist 11111111111111111111111111111111' und wenn Addition, ich bekomme die richtige Single 1? – primelf
Das Problem ist in 'subBinary' Logik, nicht die' toBinaryString' Methode –