40 std::string
addBinary(
const std::string& a,
const std::string& b) {
48 int maxLength = std::max(a.size(), b.size());
51 for (
int i = 0; i < maxLength; ++i) {
53 int bitA = (i < a.size()) ? (a[a.size() - 1 - i] -
'0') : 0;
54 int bitB = (i < b.size()) ? (b[b.size() - 1 - i] -
'0') : 0;
57 int sum = bitA + bitB + carry;
59 result.push_back((sum % 2) +
63 result.push_back(
'1');
65 std::reverse(result.begin(), result.end());