TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
binary_addition.cpp
Go to the documentation of this file.
1
18#include <algorithm>
19#include <cassert>
20#include <iostream>
21#include <string>
22
32 public:
40 std::string addBinary(const std::string& a, const std::string& b) {
42 return ""; // Return empty string if input contains non-binary
43 // characters
44 }
45
46 std::string result;
47 int carry = 0;
48 int maxLength = std::max(a.size(), b.size());
49
50 // Traverse both strings from the end to the beginning
51 for (int i = 0; i < maxLength; ++i) {
52 // Get the current bits from both strings, if available
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;
55
56 // Calculate the sum of bits and carry
57 int sum = bitA + bitB + carry;
58 carry = sum / 2; // Determine the carry for the next bit
59 result.push_back((sum % 2) +
60 '0'); // Append the sum's current bit to result
61 }
62 if (carry) {
63 result.push_back('1');
64 }
65 std::reverse(result.begin(), result.end());
66 return result;
67 }
68
69 private:
75 bool isValidBinaryString(const std::string& str) const {
76 return std::all_of(str.begin(), str.end(),
77 [](char c) { return c == '0' || c == '1'; });
78 }
79};
80} // namespace greedy_algorithms
81
86static void tests() {
88
89 // Valid binary string tests
90 assert(binaryAddition.addBinary("1010", "1101") == "10111");
91 assert(binaryAddition.addBinary("1111", "1111") == "11110");
92 assert(binaryAddition.addBinary("101", "11") == "1000");
93 assert(binaryAddition.addBinary("0", "0") == "0");
94 assert(binaryAddition.addBinary("1111", "1111") == "11110");
95 assert(binaryAddition.addBinary("0", "10101") == "10101");
96 assert(binaryAddition.addBinary("10101", "0") == "10101");
97 assert(binaryAddition.addBinary("101010101010101010101010101010",
98 "110110110110110110110110110110") ==
99 "1100001100001100001100001100000");
100 assert(binaryAddition.addBinary("1", "11111111") == "100000000");
101 assert(binaryAddition.addBinary("10101010", "01010101") == "11111111");
102
103 // Invalid binary string tests (should return empty string)
104 assert(binaryAddition.addBinary("10102", "1101") == "");
105 assert(binaryAddition.addBinary("ABC", "1101") == "");
106 assert(binaryAddition.addBinary("1010", "1102") == "");
107 assert(binaryAddition.addBinary("111", "1x1") == "");
108 assert(binaryAddition.addBinary("1x1", "111") == "");
109 assert(binaryAddition.addBinary("1234", "1101") == "");
110}
111
116int main() {
117 tests();
118 return 0;
119}
static void tests()
run self test implementation.
int main()
main function
A class to perform binary addition of two binary strings.
bool isValidBinaryString(const std::string &str) const
Validates whether a string contains only binary characters (0 or 1).
std::string addBinary(const std::string &a, const std::string &b)
Adds two binary strings and returns the result as a binary string.
for string class