We will see how to do the addition of two binary numbers. First of all, let us see what a binary string is. A binary number is a number expressed in the base-2 numeral system. Secondly, the result or the output generated by the addition of two binary numbers should also result in a binary number.
Rules For Addition
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0 (carry 1)
Binary numbers are represented using 0’s and 1’s, in the process of adding two binary numbers if the addition is to be performed on 0 and 0 (i.e., 0 + 0), then the output will be 0. Similarly, the result of performing the addition operation on 0 and 1 (i.e., 0 + 1) will be 1, and the result of performing the addition operation on 1 and 0 (i.e., 1 + 0) will be 1, finally by performing addition operation on 1 and 1 (i.e., 1 + 1) will result in 0 but carry 1.
Sample
1 0 0 + 0 1 1 = 1 1 1
0 1 1 + 0 0 1 = 1 0 0
Java Program To Add Two Binary Numbers
import java.util.Scanner;
public class addTwoBinary {
public static void main(String[] args) {
long n1,n2;
int i=0,carry=0;
int[] add = new int[20];
Scanner sc = new Scanner(System.in);
System.out.print("Enter first binary value: ");
n1 = sc.nextLong();
System.out.print("Enter second binary value: ");
n2 = sc.nextLong();
while(n1 != 0 || n2 != 0) {
add[i++] = (int)((n1 % 10 + n2 %10 + carry) %2);
carry = (int)((n1 % 10 + n2 %10 + carry) /2);
n1 = n1/10;
n2 = n2/10;
}
if(carry != 0) {
add[i++] = carry;
}
i--;
System.out.print("Output: ");
while(i>=0) {
System.out.println(add[i--]);
}
}
}
Output
Enter first binary value: 11010
Enter second binary value: 10101
Output: 101111
Explanation
Here, we used Scanner to get the input from the user.
i, carry are the temporary variables.
int add[] = new int[20];
After performing the addition operation the result is stored in the variable add and stored in the form of an array.
By using the statement n1 = sc.nextLong(); the first binary number is obtained from the user and stored in a variable called n1 where sc is the object of the scanner class. Similarly n2 = sc.nextLong(); statement will take the second binary number as input and stores in the variable n2.
A while loop is used and the condition in it is given as ‘n1 != 0 || n2 != 0’, this is to ensure that the values inside n1 and n2 are not equal to null.
add[i++] = (int)((n1 % 10 + n2 %10 + carry) %2);
The above statement will perform an addition operation on two binary values. And the result is type-casted to int.
carry = (int)((n1 % 10 + n2 %10 + carry) /2);
This statement will find if there are any carry values i.
n1 = n1/10;
n2 = n2/10;
The above statements will remove the last value from the binary number until no values were left to remove. Till now the addition process was completed, now to display the result we should use another while loop because as discussed before the result is stored in the form of an array.
while(i>=0){
System.out.println(add[i--])
}
The above lines of code will help you print the result of adding two binary number.
Conclusion
Here, we have learned about how to add two binary numbers, how to take binary numbers as input from the user, the logic for performing addition operation and finally how to print the result after performing an addition operation on two binary numbers.