I need to add binary numbers in Java. I tried on this way as below is written, the result is correct, but result is decimal number. Does anyone know how to get result as binary number?
Thanks in advance
private int number2;
private int number2;
private int result;
number1 = Byte.parseByte(String.valueOf(display.getText()));
number2 = Byte.parseByte(String.valueOf(display.getText()));
result = getDecimalFromBinary(number1) + getDecimalFromBinary(number2);
display.setText(Integer.toBinaryString(result));
Your example seems to be incomplete, because
Integer.parseInt(int, int)
andInteger.toBinaryString(int)
are what you need. Perhaps you aren't storingresult
as aString
. For example,Output is (as requested)