Adding binary strings together JavaScript


The problem is stating that we have to add two binary strings. Binary strings are a sequence of bytes. To add them in javascript, we will first convert them to decimal and calculate the sum. After adding those decimal numbers, we will convert it again into binary strings and print the output.

What are Binary Strings?

Binary numbers or binary strings are sequences of bytes in base 2. It is basically a combination of 0 and 1 to represent any number. While binary addition is one of the operations of mathematics to perform on binary strings. Binary addition works similarly to decimal addition, but with base 2.

Rule to add binary numbers-

There are some rules for adding binary numbers. If we follow these, then addition will be much easier than decimal addition. The rules for adding binary numbers are as follows:

Understanding the problem

The problem states that we have to calculate the sum of given binary strings in Javascript. To solve this problem, we will create variables to store those binary strings. After creating variables, convert them into decimal form using the parseInt() function available in javascript. Then calculate the sum of these converted numbers. And again convert it to the string format.

Algorithm

Step 1: In the first step define a function called sumOfStrings() with two arguments passed in it.

Step 2: After declaring the function, define two variables num 1 and num2. These variables are using parseInt() function to parse them in integer with redix 2.

Step 3: Add decimal numbers num1 and num2 together. And assign their value in the sum variable.

Step 4: Convert back to binary string with toString method and assign its value in binarySum.

Step 5: Finally, return the addition after all the process.

Below is the pseudocode for this algorithm

function sumOfStrings(s1, s2):
   num1 = parseInt(s1, 2)
   num2 = parseInt(s2, 2)
   addition = num1 + num2
   binarySum = addition.toString(2)
   return binarySum

Example

// Define a function to calculate sum of strings
function sumOfStrings(s1, s2) {

  // Converting binary strings to decimal numbers with parseInt
  var num1 = parseInt(s1, 2);
  var num2 = parseInt(s2, 2);

  // Add decimal numbers num1 and num2 together
  var sum = num1 + num2;

  // Converting back to binary string with toString method
  var binarySum = sum.toString(2);

  // Return string
  return binarySum;
}


//calling sumBinaryStrings function
var afterSum = sumOfStrings('0111', '1001');

//print on console
console.log("After adding two binary strings together")
console.log(afterSum); 

Output

After adding two binary strings together
10000

In the above code, we have used two predefined methods called parseInt() and toStrings(). Let's understand these methods in detail.

The parseInt() method is javascript’s predefined method. This method is used to convert binary strings into decimal numbers. For converting, we will have to pass a redix (base in the mathematical system) parameter. Redix specifies the number system to use as 2 for binary, 8 for octal, 10 for decimal, and 16 for hexadecimal.

The toString() method of javascript is used when we want to print a value in a string. In our code, sum.toString(2) converts the calculated sum into a string format.

Complexity

If we talk about time complexity for this program, it will take O(n) times to complete the execution. In this scenario, n is the length of two binary strings. Because both the functions (parseInt() and toString()) are working on the length of the given strings. And the addition operation itself is a simple operation that takes a constant amount of time to calculate the sum. So the overall complexity of this program can be calculated by adding the lengths of the strings

Conclusion

In this problem, we used and learned how to use the javascript methods parseInt and toString. And using these two methods, we have calculated the sum of two binary springs as the problem stated. Lastly, the time complexity of this algorithm is O(n) because the methods are taking lengths of strings to calculate the sum.

Updated on: 18-Aug-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements