TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
binary_addition.cpp File Reference

Adds two binary numbers and outputs resulting string. More...

#include <algorithm>
#include <cassert>
#include <iostream>
#include <string>
Include dependency graph for binary_addition.cpp:

Go to the source code of this file.

Classes

class  greedy_algorithms::BinaryAddition
 A class to perform binary addition of two binary strings. More...
 

Namespaces

namespace  greedy_algorithms
 for string class
 

Functions

static void tests ()
 run self test implementation.
 
int main ()
 main function
 

Detailed Description

Adds two binary numbers and outputs resulting string.

The algorithm for adding two binary strings works by processing them from right to left, similar to manual addition. It starts by determining the longer string's length to ensure both strings are fully traversed. For each pair of corresponding bits and any carry from the previous addition, it calculates the sum. If the sum exceeds 1, a carry is generated for the next bit. The results for each bit are collected in a result string, which is reversed at the end to present the final binary sum correctly. Additionally, the function validates the input to ensure that only valid binary strings (containing only '0' and '1') are processed. If invalid input is detected, it returns an empty string.

Author
Muhammad Junaid Khalid

Definition in file binary_addition.cpp.

Function Documentation

◆ main()

int main ( void )

main function

Returns
0 on successful exit

To execute tests

Definition at line 116 of file binary_addition.cpp.

116 {
117 tests();
118 return 0;
119}
static void tests()
run self test implementation.

◆ tests()

static void tests ( )
static

run self test implementation.

Returns
void

Definition at line 86 of file binary_addition.cpp.

86 {
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}
A class to perform binary addition of two binary strings.
std::string addBinary(const std::string &a, const std::string &b)
Adds two binary strings and returns the result as a binary string.