Skip to content

Commit 9772468

Browse files
committed
initial commit of Binary Addition
1 parent 481e5db commit 9772468

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <algorithm>
4+
#include <cassert>
5+
6+
/**
7+
* A class to perform binary addition of two binary strings.
8+
*/
9+
class Solution {
10+
public:
11+
/**
12+
* Adds two binary strings and returns the result as a binary string.
13+
*
14+
* @param a The first binary string.
15+
* @param b The second binary string.
16+
* @return The sum of the two binary strings as a binary string.
17+
*/
18+
std::string addBinary(const std::string& a, const std::string& b) {
19+
std::string result;
20+
int carry = 0;
21+
int maxLength = std::max(a.size(), b.size());
22+
23+
// Traverse both strings from the end to the beginning
24+
for (int i = 0; i < maxLength; ++i) {
25+
// Get the current bits from both strings, if available
26+
int bitA = (i < a.size()) ? (a[a.size() - 1 - i] - '0') : 0;
27+
int bitB = (i < b.size()) ? (b[b.size() - 1 - i] - '0') : 0;
28+
29+
// Calculate the sum of bits and carry
30+
int sum = bitA + bitB + carry;
31+
carry = sum / 2; // Determine the carry for the next bit
32+
result.push_back((sum % 2) + '0'); // Append the sum's current bit to result
33+
}
34+
35+
// If there's still a carry left, append it
36+
if (carry) {
37+
result.push_back('1');
38+
}
39+
40+
// The result is built in reverse order, so reverse it before returning
41+
std::reverse(result.begin(), result.end());
42+
return result;
43+
}
44+
};
45+
46+
/**
47+
* Function to run tests for the addBinary method.
48+
*/
49+
void runTests() {
50+
Solution solution;
51+
52+
// Test case for two binary strings of equal length without any carry over.
53+
assert(solution.addBinary("1010", "1101") == "10111");
54+
55+
// Test case for two binary strings of equal length with a carry over.
56+
assert(solution.addBinary("1111", "1111") == "11110");
57+
58+
// Test case for two binary strings where one is longer than the other.
59+
assert(solution.addBinary("101", "11") == "1000");
60+
61+
// Test case for a binary string of all zeros.
62+
assert(solution.addBinary("0", "0") == "0");
63+
64+
// Test case where both binary strings consist of all ones.
65+
assert(solution.addBinary("1111", "1111") == "11110");
66+
67+
// Test case where one binary string is zero and the other is non-zero.
68+
assert(solution.addBinary("0", "10101") == "10101");
69+
assert(solution.addBinary("10101", "0") == "10101");
70+
71+
// Test case for large binary numbers with many digits.
72+
assert(solution.addBinary("101010101010101010101010101010", "110110110110110110110110110110") == "1100001100001100001100001100000");
73+
74+
// Test case where one binary string is much longer than the other.
75+
assert(solution.addBinary("1", "11111111") == "100000000");
76+
77+
// Test case for adding empty strings (edge case).
78+
assert(solution.addBinary("", "") == "0");
79+
80+
// Test case where both binary strings consist of alternating ones and zeros.
81+
assert(solution.addBinary("10101010", "01010101") == "11111111");
82+
83+
std::cout << "All tests passed!" << std::endl;
84+
}
85+
86+
/**
87+
* Main function to execute the program.
88+
*/
89+
int main() {
90+
runTests();
91+
return 0;
92+
}

0 commit comments

Comments
 (0)