0

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));
  • How are you distinguishing what you consider to be a "binary number" and what you consider to be a "decimal number"? – Patricia Shanahan Jun 20 '15 at 23:11
  • *Binary numbers* contains 0 and 1. *Decimal numbers* contains 0,1,2,3,4,5,6,7,8,9. – silent_rain Jun 20 '15 at 23:14
  • Is [this](http://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#toBinaryString-int-) what you are looking for? – Turing85 Jun 20 '15 at 23:16
  • @silent_rain I don't think you are getting my point. You have a number inside a computer. It will generally be represented in binary - even BigDecimal does not actually store strings of [0,9]. What do you require to make you consider something a decimal number? – Patricia Shanahan Jun 20 '15 at 23:18
  • @PatriciaShanahan Hm, I am not sure. I tried to convert binary to decimal, Integer.toBinaryString() but it doesn't work... – silent_rain Jun 20 '15 at 23:24
  • Integer.toBinaryString() takes the internal, binary, representation of a number and outputs it as a string of zeros and ones. In what way does it "not work"? – Patricia Shanahan Jun 20 '15 at 23:29
  • 1
    It gives me result in decimal.. For example, when I put 11+11, I get 6 as result.. It is true, 11+11=110 (110=6 in decimal). But, I need binary as result, I need to get 110 not 6... – silent_rain Jun 20 '15 at 23:33
  • Well, your code works for me. I used `Integer.parseInt(String.valueOf(number1), 2)` as a replacement for your `getDecimalFromBinary(number1)`, but I get the expected result. – Tom Jun 20 '15 at 23:46
  • I tried now also with 'Integer.parseInt(String.valueOf(number1), 2)' , but I get 6 as result again... – silent_rain Jun 20 '15 at 23:58
  • What is the output of `System.out.println(Integer.toBinaryString(result));`? – Tom Jun 21 '15 at 00:06
  • @Tom On my calculator I get 6. but as output of 'System.out.println(Integer.toBinaryString(result));' I get 110... hm, it is wierd.. – silent_rain Jun 21 '15 at 00:14
  • Then `display.setText(...)` seems to be the problem. Is `display` a `JTextField`? – Tom Jun 21 '15 at 00:15
  • @Tom Yes, it is JTextField... – silent_rain Jun 21 '15 at 00:16
  • @Tom Do you suppose what it can be? – silent_rain Jun 21 '15 at 00:24
  • Well, even with a `JTextField` it works for me. Can you try to build a small program which demonstrates that problem? – Tom Jun 21 '15 at 00:25
  • possible duplicate of [Print an integer in binary format in Java](http://stackoverflow.com/questions/5263187/print-an-integer-in-binary-format-in-java) – YoYo Jun 21 '15 at 01:31
  • @Tom The program is big, I make advanced calculator... The only problem is this part of code where I need to display binary numbers... I tried to solve it in many ways, but the result is the same.. As output, I get binary, but on my display I get decimal number.... – silent_rain Jun 23 '15 at 19:20
  • But, when I put result in other JTextField, it works.... I don't know what it can be,.. maybe some settings for JTextField i changet to forbid binary numbers.... – silent_rain Jun 23 '15 at 19:50
  • If you change the values of the source numbers, do you get the correct result in decimal? Or always *6*? Is there maybe another part of code, which writes to that JTextField? – Tom Jun 23 '15 at 20:13
  • Maybe this part of code is not correct: 'if (result > -100000000 && result < 100000000) { display1.setText(Integer.toBinaryString(result)); } else { display1.setText("Error"); }' – silent_rain Jun 26 '15 at 20:51
  • Try to find each place where you set the textfield, set a brakepoint there and check which one sets the value to *6*. Might be time-consuming, but it might help. – Tom Jun 27 '15 at 20:42

1 Answers1

4

Your example seems to be incomplete, because Integer.parseInt(int, int) and Integer.toBinaryString(int) are what you need. Perhaps you aren't storing result as a String. For example,

int a = Integer.parseInt("11", 2);
int b = Integer.parseInt("11", 2);
String result = Integer.toBinaryString(a + b)
System.out.println(result);

Output is (as requested)

110
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Thanks for reply... but, i tried this, and stored result as String but I get 6 again... I don't know why is it... – silent_rain Jun 21 '15 at 00:10
  • Note that an `int` is always stored in binary. So do `long`, `float`, `double`. `BigDecimal` is not, and stored in it's base 10 form or decimal form. By default, when converting an `int` to a `String`, it will be transformed using base 10 (decimal form, or 0-9 characters). To store it as a `String` in it's binary form (1 and 0 characters), you always have to call the `Integer.toBinaryString` as shown on this answer. – YoYo Jun 21 '15 at 01:27