addition of two binary numbers in java

Solutions on MaxInterview for addition of two binary numbers in java by the best coders in the world

showing results for - "addition of two binary numbers in java"
Liam
10 Feb 2018
1import java.util.Scanner;
2public class JavaExample {
3   public static void main(String[] args)
4   {	 
5	long b1, b2;
6	int i = 0, carry = 0;
7	int[] sum = new int[10];
8	Scanner scanner = new Scanner(System.in);
9	System.out.print("Enter first binary number: ");
10	b1 = scanner.nextLong();
11	System.out.print("Enter second binary number: ");
12	b2 = scanner.nextLong();
13	scanner.close();
14	while (b1 != 0 || b2 != 0) 
15	{
16		sum[i++] = (int)((b1 % 10 + b2 % 10 + carry) % 2);
17		carry = (int)((b1 % 10 + b2 % 10 + carry) / 2);
18		b1 = b1 / 10;
19		b2 = b2 / 10;
20	}
21	if (carry != 0) {
22		sum[i++] = carry;
23	}
24	--i;
25	System.out.print("Output: ");
26	while (i >= 0) {
27		System.out.print(sum[i--]);
28	}
29	System.out.print("\n");  
30   }
31}
queries leading to this page
adding binary numbers javaadd two binary numbers in javainput two binary strings in javajava code to add binary numbersadding 2 to variable binaryhow to add 2 binary numbers in javaadding 2 binary numbers as string javaadd 2 binary numbers javaadd two binary numbers javacode to add two binary numbersadd binary numbers java without arrayhow make add 2 binary numbers javaadding binary numbers in javahow to add 2 binary numbers in java with explanation sum of binary numbers javajava sum binariesjava add two binary numbersjava add binary numbersaddition of binary numbers in javahow to add two binary numbers in java 11100 2b 00110binary addition javabinary add 1 in javaaddition of two binary numbers in java program explanationwrite a java program to add two binary numberssum of binary numbers in javawrite a java program to add two binary numbers in javabinary addition and subtraction program in javabinary addition code javabinary add 1 in java codehow to add binary numbers in javasum of 2 binary numbers program javabinary add in javaadd two binary numbers arrayjava adding binary numbersbinary number addition program in javawrite a java program to add two binary numbers add binary numbers javareplace addition by binary javawrite a java program that performs binary addition on two binary numbers use keyboard input to provide the value for the 2 binary numbers in binaddition java you have been provided with an algorithim to help you solve this question addition of binary numbers in java explainedbinary addition in javabinary addition java codecode to add 2 numbers in binary javajava binary additionjava input binaryjava program to add two binary numberssum 2 binary numbers in javaadd binary javaaddition in binary javaaddition of two binary numbers in javasum binary numbers javaadd 2 binary numbers in javajava program to add two binary numbers with explanationwrite a program to add two binary numbers in javahow binary addition owrks in javaadd two binary numbers in java explanationaddition of two binary numbers in java