← All resources

Write a program to input 2 numbers in Binary and add them.

In mathematics and digital electronics, a binary number is a number expressed in the binary numeral system or base-2 numeral system which represents numeric values using two different symbols 0s and 1s.

110100
Example of a Binary Number

There is no 2, 3, 4, 5, 6, 7, 8 or 9 in Binary!

In Order to find a binary number from a decimal number we need to divide the decimal number by 2.

Repeated Division-by-2 Method

We have seen above how to convert binary to decimal numbers, but how do we convert a decimal number into a binary number. An easy method of converting decimal to binary number equivalents is to write down the decimal number and to continually divide-by-2 (two) to give a result and a remainder of either a “1” or a “0” until the final result equals zero.

So for example.  Convert the decimal number 29410 into its binary number equivalent.

Number	294			Dividing each decimal number by “2” as shown will give a result plus a remainder.
divide by 2				
result	147	remainder	0  (LSB)	If the decimal number being divided is even then the result will be whole and the remainder will be equal to “0”. If the decimal number is odd then the result will not divide completely and the remainder will be a “1”.
divide by 2				
result	73	remainder	1	The binary result is obtained by placing all the remainders in order with the least significant bit (LSB) being at the top and the most significant bit (MSB) being at the bottom.
divide by 2				
result	36	remainder	1	
divide by 2				
result	18	remainder	0	
divide by 2				
result	9	remainder	0	
divide by 2				
result	4	remainder	1	
divide by 2				
result	2	remainder	0	
divide by 2				
result	1	remainder	0	
divide by 2				
result	0	remainder	1  (MSB)	

This divide-by-2 decimal to binary conversion technique gives the decimal number 29410 an equivalent of 1001001102 in binary, reading from right to left. This divide-by-2 method will also work for conversion to other number bases.

Then we can see that the main characteristics of a Binary Numbering System is that each “binary digit” or “bit” has a value of either “1” or “0” with each bit having a weight or value double that of its previous bit starting from the lowest or least significant bit (LSB) and this is called the “sum-of-weights” method.

Now that we know binary numbers, we will learn how to add them. Binary addition is much like your normal everyday addition (decimal addition), except that it carries on a value of 2 instead of a value of 10.

For example: in decimal addition, if you add 8 + 2 you get ten, which you write as 10; in the sum this gives a digit 0 and a carry of 1. Something similar happens in binary addition when you add 1 and 1; the result is two (as always), but since two is written as 10 in binary, we get, after summing 1 + 1 in binary, a digit 0 and a carry of 1.

Therefore in binary:
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 10 (which is 0 carry 1)

Problem: 100101 oplus 010101 = ?.

Answer: 100101 oplus 010101 = 110000.

Explanation:

1 0 0 1 0 1
oplus 0 1 0 1 0 1
first column (from the right) : 1 oplus 1 = 0
second column : 0 oplus 0 = 0
third column : 1 oplus 1 = 0
fourth column : 0 oplus 0 = 0
fifth column : 0 oplus 1 = 1
sixth column : 1 oplus 0 = 1

So our answer is: 1 1 0 0 0 0!

Output

Enter the two binary numbers: 10011 11001 Result: 101100

import java.io.*;
public class binary_add
{
    public boolean isValid(int n)
    {
        int d;
        while(n!=0)
        {
            d = n%10;
            n = n/10;
            if(d!=0&&d!=1)
            return false;
        }
        return true;
    }
    public int sum(int x, int y, int c)
    {
        int s = x+y+c;
        return s%2;
    }
    public int carry(int x, int y, int c)
    {
        int s = x+y+c;
        return s/2;
    }
    public void calc(int a, int b)
    {
        String b1 = ""+a, b2 = ""+b, r="";
        int d1, d2, c=0, i, l;
        if(b1.length()<b2.length()) { r = b1; b1 = b2; b2 = r; } r=""; l = b2.length()-1; for(i=b1.length()-1;i>=0;i--)
        {
            d1 = b1.charAt(i)-48;
            if(l>=0)
            {
                d2 = b2.charAt(l)-48;
                l--;
            }
            else
            d2 = 0;
            r = sum(d1, d2, c)+r;
            c = carry(d1, d2, c);
        }
        if(c!=0)
        r = c+r;
        System.out.println("Result:"+r);
    }
    public static void main()throws IOException
    {
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        binary_add ob = new binary_add();
        int x, y;
        System.out.println("Enter the two binary numbers:");
        x = Integer.parseInt(r.readLine());
        y = Integer.parseInt(r.readLine());
        if(ob.isValid(x)&&ob.isValid(y))
        {
            ob.calc(x, y);
        }
        else
        {
            System.out.println("The input is incorrect.");
        }
    }
}