简体   繁体   中英

How to input and add two binary numbers? [Java]

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");  
 }
} 

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.

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM