Java Program to Add Two Binary Strings
When two binary strings are added, then the sum returned is also a binary string.
Example:
Input : x = "10", y = "01" Output: "11" Input : x = "110", y = "011" Output: "1001" Explanation: 110 + 011 =1001
Here, we need to start adding from the right side and when the sum returned is more than one then store the carry for the next digits.
Let’s see a program in order to get the clear concept of above topic.
Example:
Java
// java program to add two binary strings public class gfg { // Function to add two binary strings static String add_Binary(String x, String y) { int num1 = Integer.parseInt(x, 2 ); //converting binary string into integer(decimal number) int num2 = Integer.parseInt(y, 2 ); //converting binary string into integer(decimal number) int sum = num1 + num2; // Adding those two decimal numbers and storing in sum String result = Integer.toBinaryString(sum); //Converting that resultant decimal into binary string return result; } // The Driver code public static void main(String args[]) { String x = "011011" , y = "1010111" ; System.out.print(add_Binary(x, y)); } } |
Output
1110010
Please Login to comment...