-2

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)
  '''
rupesh raj
  • 17
  • 5
  • You posted an assignment and some code. What is the problem or question? – takendarkk May 12 '20 at 15:16
  • Actually i i want to multiply two binary string like if is multiply without converting into string because when i convert it in the integer then multiply and again convert it into binary the output is different ..please help me out – rupesh raj May 12 '20 at 15:23

2 Answers2

2

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)
}
Mike
  • 3,855
  • 5
  • 30
  • 39
FC1927DK
  • 21
  • 4
  • when we run this code the output is 1.0011011110110101e+26 for the input "01011001010101","11011111010101" .... we just multiply these two string without converting into binary this is the issue. – rupesh raj May 12 '20 at 15:52
1

Use BigInt in js

var output = (BigInt(parseInt("01011001010101",2)) * BigInt(parseInt("11011111010101",2))).toString(2);
// --> 100110111101101011110111001
smith john
  • 33
  • 9