-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_binary_strings.cpp
More file actions
57 lines (53 loc) · 1.22 KB
/
Copy pathadd_binary_strings.cpp
File metadata and controls
57 lines (53 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//Given two binary strings, return their sum (also a binary string).
#include<iostream>
#include<cstdlib>
#include<vector>
#include<cstring>
#include<bits/stdc++.h>
using namespace std;
/*Input: a = "11", b = "1"
Output: "100"*/
std::string addBinaryStrings(std::string a, std::string b)
{
string result="";
int i=a.size()-1;
int j=b.size()-1;
int carry_in=0;
while(i>=0 && j>=0)
{
int a1=a[i]-'0';
int b1=b[j]-'0';
int sum=a1+b1+carry_in;
carry_in=sum/2;
result+=char((sum%2)+'0');
i--;
j--;
}
if(i!=-1)
for(int k=i;k>=0;k--)
{
int sum=(a[k]-'0')+carry_in;
result+=char((sum%2)+'0');
carry_in=sum/2;
}
if(j!=-1)
for(int k=j;k>=0;k--)
{
int sum=(b[k]-'0')+carry_in;
result+=char((sum%2)+'0');
carry_in=sum/2;
}
if(carry_in==1)
result+=char(carry_in+'0');
reverse(result.begin(),result.end());
return result;
}
int main()
{
// std::string a="1010110111001101101000";
// std::string b="1000011011000000111100110";
std::string a="10101011";
std::string b="110001111110101";
std::cout<<"The sum is : \n"<<addBinaryStrings(a,b)<<"\n";
return 0;
}