Posted on by Kalkicode
Code String

Addition of binary strings

Here given code implementation process.

/*
    Java Program for
    Addition of binary strings
*/
public class Addition
{
    public void addBinary(String a, String b)
    {
        String result = "";
        // Get the length of given number
        int x = a.length() - 1;
        int y = b.length() - 1;
        int sum = 0;

        while (x >= 0 || y >= 0 || sum == 1)
        {
            if (x >= 0)
            {
                // Sum of binary digit at position x 
                sum += (a.charAt(x) - '0');
                x--;
            }
            if (y >= 0)
            {
                // Sum of binary digit at position y 
                sum += (b.charAt(y) - '0');
                y--;
            }
            result = (char)((sum % 2)+'0') + result;

            sum = sum / 2;
         
        }
        // Display given value
        System.out.println("Given a : " + a);
        System.out.println("Given b : " + b);
        // Display addition result
        System.out.println("Result  : " + result);
    }
    public static void main(String[] args)
    {
        Addition task = new Addition();
        // Test input
        //   0101      5
        //    111      7
        // -----------------
        //   1100      12
        task.addBinary("0101", "111");
        //   101011    43
        //     1010    10
        // ------------------
        //   110101    53
        task.addBinary("101011", "1010");    
        //   1111      15
        //   1111      15
        // ------------------
        //  11110      30
        task.addBinary("1111", "1111");
    }
}

Output

Given a : 0101
Given b : 111
Result  : 1100
Given a : 101011
Given b : 1010
Result  : 110101
Given a : 1111
Given b : 1111
Result  : 11110
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ Program for
    Addition of binary strings
*/
class Addition
{
	public: void addBinary(string a, string b)
	{
		string result = "";
		// Get the length of given number
		int x = a.length() - 1;
		int y = b.length() - 1;
		int sum = 0;
		while (x >= 0 || y >= 0 || sum == 1)
		{
			if (x >= 0)
			{
				// Sum of binary digit at position x 
				sum += (a[x] - '0');
				x--;
			}
			if (y >= 0)
			{
				// Sum of binary digit at position y 
				sum += (b[y] - '0');
				y--;
			}
			result = ((char)((sum % 2) + '0'))  +  result;
			sum = sum / 2;
		}
		// Display given value
		cout << "Given a : " << a << endl;
		cout << "Given b : " << b << endl;
		// Display addition result
		cout << "Result  : " << result << endl;
	}
};
int main()
{
	Addition *task = new Addition();
	// Test input
	//   0101      5
	//    111      7
	// -----------------
	//   1100      12
	task->addBinary("0101", "111");
	//   101011    43
	//     1010    10
	// ------------------
	//   110101    53
	task->addBinary("101011", "1010");
	//   1111      15
	//   1111      15
	// ------------------
	//  11110      30
	task->addBinary("1111", "1111");
	return 0;
}

Output

Given a : 0101
Given b : 111
Result  : 1100
Given a : 101011
Given b : 1010
Result  : 110101
Given a : 1111
Given b : 1111
Result  : 11110
// Include namespace system
using System;
/*
    Csharp Program for
    Addition of binary strings
*/
public class Addition
{
	public void addBinary(String a, String b)
	{
		String result = "";
		// Get the length of given number
		int x = a.Length - 1;
		int y = b.Length - 1;
		int sum = 0;
		while (x >= 0 || y >= 0 || sum == 1)
		{
			if (x >= 0)
			{
				// Sum of binary digit at position x 
				sum += (a[x] - '0');
				x--;
			}
			if (y >= 0)
			{
				// Sum of binary digit at position y 
				sum += (b[y] - '0');
				y--;
			}
			result = (char)((sum % 2) + '0') + result;
			sum = sum / 2;
		}
		// Display given value
		Console.WriteLine("Given a : " + a);
		Console.WriteLine("Given b : " + b);
		// Display addition result
		Console.WriteLine("Result  : " + result);
	}
	public static void Main(String[] args)
	{
		Addition task = new Addition();
		// Test input
		//   0101      5
		//    111      7
		// -----------------
		//   1100      12
		task.addBinary("0101", "111");
		//   101011    43
		//     1010    10
		// ------------------
		//   110101    53
		task.addBinary("101011", "1010");
		//   1111      15
		//   1111      15
		// ------------------
		//  11110      30
		task.addBinary("1111", "1111");
	}
}

Output

Given a : 0101
Given b : 111
Result  : 1100
Given a : 101011
Given b : 1010
Result  : 110101
Given a : 1111
Given b : 1111
Result  : 11110
package main

import "fmt"
/*
    Go Program for
    Addition of binary strings
*/

func addBinary(a, b string) {
	var result string = ""
	// Get the length of given number
	var x int = len(a) - 1
	var y int = len(b) - 1
	var sum int = 0
	for (x >= 0 || y >= 0 || sum == 1) {
		if x >= 0 {
			// Sum of binary digit at position x 
			sum += (int(a[x]) - 48)
			x--
		}
		if y >= 0 {
			// Sum of binary digit at position y 
			sum += (int(b[y]) - 48)
			y--
		}
		result = string(((sum % 2) + 48)) + result
		sum = sum / 2
	}
	// Display given value
	fmt.Println("Given a : ", a)
	fmt.Println("Given b : ", b)
	// Display addition result
	fmt.Println("Result  : ", result)
}
func main() {

	// Test input
	//   0101      5
	//    111      7
	// -----------------
	//   1100      12
	addBinary("0101", "111")
	//   101011    43
	//     1010    10
	// ------------------
	//   110101    53
	addBinary("101011", "1010")
	//   1111      15
	//   1111      15
	// ------------------
	//  11110      30
	addBinary("1111", "1111")
}

Output

Given a : 0101
Given b : 111
Result  : 1100
Given a : 101011
Given b : 1010
Result  : 110101
Given a : 1111
Given b : 1111
Result  : 11110
<?php
/*
    Php Program for
    Addition of binary strings
*/
class Addition
{
	public	function addBinary($a, $b)
	{
		$result = "";
		// Get the length of given number
		$x = strlen($a) - 1;
		$y = strlen($b) - 1;
		$sum = 0;
		while ($x >= 0 || $y >= 0 || $sum == 1)
		{
			if ($x >= 0)
			{
				// Sum of binary digit at position x 
				$sum += (ord($a[$x]) - ord('0'));
				$x--;
			}
			if ($y >= 0)
			{
				// Sum of binary digit at position y 
				$sum += (ord($b[$y]) - ord('0'));
				$y--;
			}
			$result = strval(chr((($sum % 2) + ord('0')))).$result;
			$sum = (int)($sum / 2);
		}
		// Display given value
		echo("Given a : ".$a."\n");
		echo("Given b : ".$b."\n");
		// Display addition result
		echo("Result  : ".$result."\n");
	}
}

function main()
{
	$task = new Addition();
	// Test input
	//   0101      5
	//    111      7
	// -----------------
	//   1100      12
	$task->addBinary("0101", "111");
	//   101011    43
	//     1010    10
	// ------------------
	//   110101    53
	$task->addBinary("101011", "1010");
	//   1111      15
	//   1111      15
	// ------------------
	//  11110      30
	$task->addBinary("1111", "1111");
}
main();

Output

Given a : 0101
Given b : 111
Result  : 1100
Given a : 101011
Given b : 1010
Result  : 110101
Given a : 1111
Given b : 1111
Result  : 11110
/*
    Node JS Program for
    Addition of binary strings
*/
class Addition
{
	addBinary(a, b)
	{
		var result = "";
		// Get the length of given number
		var x = a.length - 1;
		var y = b.length - 1;
		var sum = 0;
		while (x >= 0 || y >= 0 || sum == 1)
		{
			if (x >= 0)
			{
				// Sum of binary digit at position x 
				sum += (a.charCodeAt(x) - '0'.charCodeAt(0));
				x--;
			}
			if (y >= 0)
			{
				// Sum of binary digit at position y 
				sum += (b.charCodeAt(y) - '0'.charCodeAt(0));
				y--;
			}
			result = String.fromCharCode(((sum % 2) + '0'.charCodeAt(0))) + result;
			sum = parseInt(sum / 2);
		}
		// Display given value
		console.log("Given a : " + a);
		console.log("Given b : " + b);
		// Display addition result
		console.log("Result  : " + result);
	}
}

function main()
{
	var task = new Addition();
	// Test input
	//   0101      5
	//    111      7
	// -----------------
	//   1100      12
	task.addBinary("0101", "111");
	//   101011    43
	//     1010    10
	// ------------------
	//   110101    53
	task.addBinary("101011", "1010");
	//   1111      15
	//   1111      15
	// ------------------
	//  11110      30
	task.addBinary("1111", "1111");
}
main();

Output

Given a : 0101
Given b : 111
Result  : 1100
Given a : 101011
Given b : 1010
Result  : 110101
Given a : 1111
Given b : 1111
Result  : 11110
#    Python 3 Program for
#    Addition of binary strings
class Addition :
	def addBinary(self, a, b) :
		result = ""
		#  Get the length of given number
		x = len(a) - 1
		y = len(b) - 1
		sum = 0
		while (x >= 0 or y >= 0 or sum == 1) :
			if (x >= 0) :
				#  Sum of binary digit at position x 
				sum += (ord(a[x]) - ord('0'))
				x -= 1
			
			if (y >= 0) :
				#  Sum of binary digit at position y 
				sum += (ord(b[y]) - ord('0'))
				y -= 1
			
			result = str(chr(((sum % 2) + ord('0')))) + result
			sum = int(sum / 2)
		
		#  Display given value
		print("Given a : ", a)
		print("Given b : ", b)
		#  Display addition result
		print("Result  : ", result)
	

def main() :
	task = Addition()
	#  Test input
	#    0101      5
	#     111      7
	#  -----------------
	#    1100      12
	task.addBinary("0101", "111")
	#    101011    43
	#      1010    10
	#  ------------------
	#    110101    53
	task.addBinary("101011", "1010")
	#    1111      15
	#    1111      15
	#  ------------------
	#   11110      30
	task.addBinary("1111", "1111")

if __name__ == "__main__": main()

Output

Given a :  0101
Given b :  111
Result  :  1100
Given a :  101011
Given b :  1010
Result  :  110101
Given a :  1111
Given b :  1111
Result  :  11110
#    Ruby Program for
#    Addition of binary strings
class Addition 
	def addBinary(a, b) 
		result = ""
		#  Get the length of given number
		x = a.length - 1
		y = b.length - 1
		sum = 0
		while (x >= 0 || y >= 0 || sum == 1) 
			if (x >= 0) 
				#  Sum of binary digit at position x 
				sum += (a[x].ord - '0'.ord)
				x -= 1
			end

			if (y >= 0) 
				#  Sum of binary digit at position y 
				sum += (b[y].ord - '0'.ord)
				y -= 1
			end

			result = (((sum % 2) + '0'.ord)).chr.to_s + result
			sum = sum / 2
		end

		#  Display given value
		print("Given a : ", a, "\n")
		print("Given b : ", b, "\n")
		#  Display addition result
		print("Result  : ", result, "\n")
	end

end

def main() 
	task = Addition.new()
	#  Test input
	#    0101      5
	#     111      7
	#  -----------------
	#    1100      12
	task.addBinary("0101", "111")
	#    101011    43
	#      1010    10
	#  ------------------
	#    110101    53
	task.addBinary("101011", "1010")
	#    1111      15
	#    1111      15
	#  ------------------
	#   11110      30
	task.addBinary("1111", "1111")
end

main()

Output

Given a : 0101
Given b : 111
Result  : 1100
Given a : 101011
Given b : 1010
Result  : 110101
Given a : 1111
Given b : 1111
Result  : 11110
/*
    Scala Program for
    Addition of binary strings
*/
class Addition()
{
	def addBinary(a: String, b: String): Unit = {
		var result: String = "";
		// Get the length of given number
		var x: Int = a.length() - 1;
		var y: Int = b.length() - 1;
		var sum: Int = 0;
		while (x >= 0 || y >= 0 || sum == 1)
		{
			if (x >= 0)
			{
				// Sum of binary digit at position x 
				sum += (a.charAt(x).toInt - '0'.toInt);
				x -= 1;
			}
			if (y >= 0)
			{
				// Sum of binary digit at position y 
				sum += (b.charAt(y).toInt - '0'.toInt);
				y -= 1;
			}
			result = ((sum % 2) + '0'.toInt).toChar.toString() + result;
			sum = sum / 2;
		}
		// Display given value
		println("Given a : " + a);
		println("Given b : " + b);
		// Display addition result
		println("Result  : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Addition = new Addition();
		// Test input
		//   0101      5
		//    111      7
		// -----------------
		//   1100      12
		task.addBinary("0101", "111");
		//   101011    43
		//     1010    10
		// ------------------
		//   110101    53
		task.addBinary("101011", "1010");
		//   1111      15
		//   1111      15
		// ------------------
		//  11110      30
		task.addBinary("1111", "1111");
	}
}

Output

Given a : 0101
Given b : 111
Result  : 1100
Given a : 101011
Given b : 1010
Result  : 110101
Given a : 1111
Given b : 1111
Result  : 11110
/*
    Kotlin Program for
    Addition of binary strings
*/
class Addition
{
	fun addBinary(a: String, b: String): Unit
	{
		var result: String = "";
		// Get the length of given number
		var x: Int = a.length - 1;
		var y: Int = b.length - 1;
		var sum: Int = 0;
		while (x >= 0 || y >= 0 || sum == 1)
		{
			if (x >= 0)
			{
				// Sum of binary digit at position x 
				sum += (a.get(x).toInt() - '0'.toInt());
				x -= 1;
			}
			if (y >= 0)
			{
				// Sum of binary digit at position y 
				sum += (b.get(y).toInt() - '0'.toInt());
				y -= 1;
			}
			result = ((sum % 2) + '0'.toInt()).toChar().toString() + result;
			sum = sum / 2;
		}
		// Display given value
		println("Given a : " + a);
		println("Given b : " + b);
		// Display addition result
		println("Result  : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Addition = Addition();
	// Test input
	//   0101      5
	//    111      7
	// -----------------
	//   1100      12
	task.addBinary("0101", "111");
	//   101011    43
	//     1010    10
	// ------------------
	//   110101    53
	task.addBinary("101011", "1010");
	//   1111      15
	//   1111      15
	// ------------------
	//  11110      30
	task.addBinary("1111", "1111");
}

Output

Given a : 0101
Given b : 111
Result  : 1100
Given a : 101011
Given b : 1010
Result  : 110101
Given a : 1111
Given b : 1111
Result  : 11110

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment