2 binary strings. The function will multiply these two arguments and return the result as a binary string

1.2k views Asked by At

And Write a JS function that takes 2 arguments: 2 binary strings. The function will multiply these two arguments and return the result as a binary string.

    Note_: A binary string is a sequence of bytes, such as `"1001001100"`
   input= ("01011001010101", "11011111010101"),
     output= 100110111101101011110111001
For Example :   
str1 = `"10"`  
str2 = `"100"`  
Output = `"1000"`


 '''
    function raj(n1,n2){
    var n1=n1.split("")
    var n2=n2.split("")
    var mul=[]
    for(var i=0;i<n1.length;i++){
     for(var ;j<n2.length;j--){
      var k=n1[i]*n2[j]
     mul.push(k)
     }
    }
     return mul
    }
    var x=raj("10","100")
    console.log(x)
  '''
2

There are 2 answers

1
smith john On

Use BigInt in js

var output = (BigInt(parseInt("01011001010101",2)) * BigInt(parseInt("11011111010101",2))).toString(2);
// --> 100110111101101011110111001
1
FC1927DK On

Here is a function that multiplies two binary strings and returns the output as a binary string:

function multiplyBinaryStrings (binaryString1, binaryString2){
    let int1 = parseInt(binaryString1, 2)
    let int2 = parseInt(binaryString2, 2)
    return (int1 * int2).toString(2)
}