1
- #include < iostream>
2
- #include < string>
3
1
#include < algorithm>
4
2
#include < cassert>
3
+ #include < iostream>
4
+ #include < string>
5
5
6
6
/* *
7
7
* A class to perform binary addition of two binary strings.
8
8
*/
9
9
class Solution {
10
- public:
10
+ public:
11
11
/* *
12
12
* Adds two binary strings and returns the result as a binary string.
13
13
*
@@ -28,8 +28,9 @@ class Solution {
28
28
29
29
// Calculate the sum of bits and carry
30
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
31
+ carry = sum / 2 ; // Determine the carry for the next bit
32
+ result.push_back ((sum % 2 ) +
33
+ ' 0' ); // Append the sum's current bit to result
33
34
}
34
35
35
36
// If there's still a carry left, append it
@@ -69,15 +70,18 @@ void runTests() {
69
70
assert (solution.addBinary (" 10101" , " 0" ) == " 10101" );
70
71
71
72
// Test case for large binary numbers with many digits.
72
- assert (solution.addBinary (" 101010101010101010101010101010" , " 110110110110110110110110110110" ) == " 1100001100001100001100001100000" );
73
+ assert (solution.addBinary (" 101010101010101010101010101010" ,
74
+ " 110110110110110110110110110110" ) ==
75
+ " 1100001100001100001100001100000" );
73
76
74
77
// Test case where one binary string is much longer than the other.
75
78
assert (solution.addBinary (" 1" , " 11111111" ) == " 100000000" );
76
79
77
80
// Test case for adding empty strings (edge case).
78
- assert (solution.addBinary (" " , " " ) == " 0 " );
81
+ assert (solution.addBinary (" " , " " ) == " " );
79
82
80
- // Test case where both binary strings consist of alternating ones and zeros.
83
+ // Test case where both binary strings consist of alternating ones and
84
+ // zeros.
81
85
assert (solution.addBinary (" 10101010" , " 01010101" ) == " 11111111" );
82
86
83
87
std::cout << " All tests passed!" << std::endl;
0 commit comments