Important Java Concept

Addition Of Two Binary Strings in Java

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)

As we learned that 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.

Java Program To Add Two Binary Strings

public class AddBinaryStrings{

    static String addBinary(String n1, String n2) {
        StringBuilder sum = new StringBuilder("");
        int x = 0;
        int i = n1.length() - 1;
        int j = n2.length() - 1;
        while (i >= 0 || j >= 0 || x == 1) {
            x += ((i > 0) ? n1.charAt(i) - '0' : 0);
            x += ((j > 0) ? n2.charAt(j) - '0' : 0);
            sum.append((char) (x % 2 + '0'));
            x = x / 2;
            i--;
            j--;
        }
        return sum.toString();
    }


    public static void main(String[] args) {
        String n1 = "1010001", n2 = "1101011";
        System.out.println(addBinary(n1, n2));
    }
}

Output

0011110

Explanation

We have created a static method called addBinary which takes two binary strings as input through parameters.

StringBuilder sum = new StringBuilder("");

The above statement is used to create a StringBuilder object named sum and empty input was given to it.

int i = n1.length() -1;
int j = n2.length() -1;

The above lines of code are used to find the lengths of binary strings. n1 binary string length is stored in “i” and n2 binary string length is stored in “j”.

while(i>=0|| j>=0||x == 1)

Here the while loop is used to repeat the addition process till there is no possibility for further addition. This while loop executes until the value of i becomes less than 0 (or) the value of j becomes less than 0 (or) the value of x is equal to 1.

x+=((i>0) ? n1.charAt(i) - '0' : 0);
x+=((j>0) ? n2.charAt(j) - '0' : 0);
sum.append((char)(x % 2 + '0'));
x=x/2;
i--;
j--;

Here in the above lines of the code addition operation is done. After performing the addition operation, the result is type-casted to a character from the string. Later we used the statement x=x/2; because this statement will remove the last character from the input BinaryString until it reaches to the end( right to left). Now i and j values are decremented.

In the Main method, we have declared two binary values as strings and passed them into the method ( addBinary ) as parameters inside the print statement.

Conclusion

In this article, we have seen learned about how to add two binary strings, the logic of the program, how to send binary strings as parameters, and how to print the result by passing parameters inside the print statement.

Do you want

Java Tutor?

Aniket Malik

Aniket Malik

ORACLE CERTIFIED JAVA TUTOR

🟢 Oracle Certified Java Expert

🟢 300 + ⭐ ⭐ ⭐ ⭐ ⭐

🟢 B.tech & M.tech in CSE

🟢 6+ Years of Teaching Experience

🟢 Worked as SE in Virtusa and Digidez

Do you want

Java Tutor?