1

I am beginner at Java. While doing some exercises on w3resource i came across on problem that puzzles me. Basically I need to input two binary numbers and add them. I would like to know is there easier way to do this without inputing binary numbers as long variables,(as it is in given solution to this exercise) and if there is not easier way could someone explain part where while loop is used to the end of the code?

Thanks in advance!

/*given solution*/
import java.util.Scanner;
public class Exercise17 {
 public static void main(String[] args)
 {
 long binary1, binary2;
 int i = 0, remainder = 0;
 int[] sum = new int[20];
 Scanner in = new Scanner(System.in);
 System.out.print("Input first binary number: ");
 binary1 = in.nextLong();
 System.out.print("Input second binary number: ");
 binary2 = in.nextLong();
 while (binary1 != 0 || binary2 != 0) 
 {
 sum[i++] = (int)((binary1 % 10 + binary2 % 10 + remainder) % 2);
 remainder = (int)((binary1 % 10 + binary2 % 10 + remainder) / 2);
 binary1 = binary1 / 10;
 binary2 = binary2 / 10;
 }
 if (remainder != 0) {
 sum[i++] = remainder;
 }
 --i;
 System.out.print("Sum of two binary numbers: ");
 while (i >= 0) {
 System.out.print(sum[i--]);
 }
 System.out.print("\n"); 
 }
} 
asked Apr 16, 2020 at 11:46
1
  • 1
    Read the numbers in as Strings instead. Then you can just pull the "1"s and "0"s directly out of them, instead of doing all that remaindering and dividing by 10. And you wouldn't be limited to the 18 or so digits that a long can hold. Commented Apr 16, 2020 at 12:07

4 Answers 4

2

I think an easier way might be to read in your binary numbers as String's and then do the following

String b1 = "101";
String b2 = "1000";
BigInteger bi1 = new BigInteger(b1, 2);
BigInteger bi2 = new BigInteger(b2, 2);
BigInteger result = bi1.add(bi2);
System.out.println(result.longValue());
System.out.println(result.toString(2));

The last two lines show the output in decimal and then binary.

answered Apr 16, 2020 at 12:15

Comments

1

They use longs and interpret them to act as binary numbers, eg.: if you input 10 it will be the binary representation of the decimal 2. So they use long as the easiest way to input those numbers.

Now for the while Loop:

(binary1 % 10 + binary2 % 10 + remainder)

This takes the last digits (using modulo % 10) and sums them up. The result may have two digits (in binary-system). The last digit is taken using again modulo (% 2) and put in result-array. The first digit is put in ramainder to add it to next digit.

answered Apr 16, 2020 at 12:04

Comments

1

There are two ways you can do it easily by using Integer wrapper class.

Integer.toString(0b+yournum + 0b+othernumber,2);

Eg:-

Integer.toString(0b101 + 0b110 ,2); 

Requires java 7 and above.

Take input as String and use parseInt method

int n1 = "101";
int n2 = "110";
int sum = Integer.parseInt(n1,2) + Integer.parseInt(n2,2);
Integer.toBinaryString(sum);

Ps:- This methods are limited to max int size 2147483647 else it will throw exception

answered Apr 16, 2020 at 12:19

Comments

0

You can easily take the binary input using Scanner.

 Scanner scan = new Scanner(System.in);
 System.out.print("Enter first binary number :: ");
 // To take binray input you just need to write 2 as a base value
 int b1 = scan.nextInt(2);
 System.out.print("Enter second binary number :: ");
 int b2 = scan.nextInt(2);
 int result = b1 + b2;
 System.out.println(result);

This works for me.

answered May 21, 2021 at 9:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.