Open In App

JavaScript Program to Add Two Binary Strings

Last Updated : 20 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see the addition operation on two binary Strings in JavaScript. The addition of two binary strings in JavScript is the concept of performing binary addition on a collection of two binary strings, treating them as binary numbers (1 or 0), and then printing the sum of the binary representation as the final result.

Using parseInt() and toString() Methods

The parseInt() method used here first converts the strings into the decimal. Ten of these converted decimal values are added together and by using the toString() method, we convert the sum back to the desired binary representation.

Syntax:

parseInt(str1,2) + parseInt(str1,2).toString(2);

Example: In this example, we will see the the addtion of two binary strings using parseInt() and toString() methods.

Javascript
let str1 = "101010";
let str2 = "1011";
let sum = (
    parseInt(str1, 2) +
    parseInt(str2, 2)
).toString(2);
console.log(sum);

Output
110101

Using BigInt Method

The approach uses the BigInt method where the binary strings are convereted into BigInt integers, then addition is performed and once again the conversion of the sum is to binary string. This is used to handle large binary numbers.

Syntax:

let bigIntValue = BigInt(value);

Example: In this example, we will see the addtion of two binary strings using BigInt method.

Javascript
let str1 = "101010";
let str2 = "1011";
let sum = (a, b) => {
    let decSum =
        BigInt(`0b${a}`) +
        BigInt(`0b${b}`);
    return decSum.toString(2);
};
console.log(sum(str1, str2));

Output
110101

Using Manual Operation

The manual operations add the binary digits. We initially ensuring that both of the strings are of same length by adding them with leading 0s, and then iterating through the strings from left to right order and adding binary digits while considering the carry.

Syntax:

 let sum= (a, b) => {
//initialization

for (let i = maxLength - 1; i >= 0; i--) {
// addtion statements

}
return carry ? '1' + result : result;
};

Example: In this example, we will be performing addtion of two binary strings without using any inbuilt method.

Javascript
let str1 = "101010";
let str2 = "1011";
let sum = (m, n) => {
    let len = Math.max(
        m.length,
        n.length
    );
    m = m.padStart(len, "0");
    n = n.padStart(len, "0");
    let carry = 0;
    let res = "";

    for (let i = len - 1; i >= 0; i--) {
        let mBit = +m[i];
        let nBit = +n[i];
        let sum = mBit + nBit + carry;
        carry = Math.floor(sum / 2);
        res = (sum % 2) + res;
    }
    return carry ? "1" + res : res;
};
console.log(sum(str1, str2));

Output
110101

Using Recursive Approach

The recursive approach adds two binary strings by breaking down the problem into smaller subproblems. It handles the binary addition bit-by-bit from the end, carrying over the extra bit when necessary, until the base case where one of the strings is empty.

Example: In this example The addBinary function adds two binary strings together and returns their sum as a binary string. It recursively calculates the sum bit by bit, handling carryovers.

JavaScript
function addBinary(a, b) {
    if (a.length < b.length) [a, b] = [b, a];
    if (b.length === 0) return a;
    if (a[a.length - 1] === '1' && b[b.length - 1] === '1') {
        return addBinary(addBinary(a.slice(0, -1), b.slice(0, -1)), '1') + '0';
    }
    if (a[a.length - 1] === '0' && b[b.length - 1] === '0') {
        return addBinary(a.slice(0, -1), b.slice(0, -1)) + '0';
    }
    return addBinary(a.slice(0, -1), b.slice(0, -1)) + '1';
}

console.log(addBinary("1010", "1011"));

Output
10101


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads