TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
binary_addition.cpp
Go to the documentation of this file.
1
17
18
#include <algorithm>
19
#include <cassert>
20
#include <iostream>
21
#include <string>
22
27
namespace
greedy_algorithms
{
31
class
BinaryAddition
{
32
public
:
40
std::string
addBinary
(
const
std::string& a,
const
std::string& b) {
41
if
(!
isValidBinaryString
(a) || !
isValidBinaryString
(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
86
static
void
tests
() {
87
greedy_algorithms::BinaryAddition
binaryAddition;
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
116
int
main
() {
117
tests
();
118
return
0;
119
}
greedy_algorithms::BinaryAddition
A class to perform binary addition of two binary strings.
Definition
binary_addition.cpp:31
greedy_algorithms::BinaryAddition::isValidBinaryString
bool isValidBinaryString(const std::string &str) const
Validates whether a string contains only binary characters (0 or 1).
Definition
binary_addition.cpp:75
greedy_algorithms::BinaryAddition::addBinary
std::string addBinary(const std::string &a, const std::string &b)
Adds two binary strings and returns the result as a binary string.
Definition
binary_addition.cpp:40
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
greedy_algorithms
for string class
Definition
binary_addition.cpp:27
tests
Testcases to check Union of Two Arrays.
greedy_algorithms
binary_addition.cpp
Generated by
1.18.0