Add Binary

Desicription

Given two binary strings, return their sum (also a binary string).

For example,

a = "11"

b = "1"

Return "100".

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
string addBinary(string a, string b) {
string res;
int a_index = a.size() - 1;
int b_index = b.size() - 1;
int carry = 0;
while(a_index >= 0 || b_index >= 0 || carry){
carry += a_index >= 0? a[a_index--] - '0' : 0;
carry += b_index >= 0? b[b_index--] - '0' : 0;
res = char (carry % 2 + '0') + res;
carry >>= 1;
}
return res;
}
};