-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryString.cpp
More file actions
28 lines (21 loc) · 889 Bytes
/
binaryString.cpp
File metadata and controls
28 lines (21 loc) · 889 Bytes
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
int n = s1.length();
int m = s2.length();
int carry = 0; // To keep track of the carry
string res = ""; // Result string
int i = n - 1, j = m - 1;
// Traverse both strings from the rightmost bit
while (i >= 0 || j >= 0 || carry) {
int bit1 = (i >= 0) ? s1[i] - '0' : 0; // Bit from s1
int bit2 = (j >= 0) ? s2[j] - '0' : 0; // Bit from s2
int sum = bit1 + bit2 + carry; // Sum of bits and carry
// Append the resultant bit to the result
res = char((sum % 2) + '0') + res;
// Update carry
carry = sum / 2;
// Move to the next bits
i--;
j--;
}
res.erase(0, res.find_first_not_of('0'));
// If result is empty after removing zeros, return "0"
return res.empty() ? "0" : res;