I needed to write a program which adds binary numbers as if they were decimal. But it isn't working like I expected.
int i = 0101, j=0001;
System.out.println(i+j);
I expected the answer to be either 6 (i.e decimal of sum of 0101 and 0001) or maybe 0102 (as I am adding them as simple decimal numbers). But unexpectedly, I am getting 66. Can anybody kindly explain this? Or may be help me with the code to add two binary numbers as decimal numbers.
First we need to convert the number from binary to decimal. And for that you need to parse it using the parse commands for the
Integer
wrapper class. Then you can add.The format
Integer.parseInt(String,radix)
can be used to convert any string of digits to the baseradix
.