From 2e59fe6aa585776d810fa265a6706fcbe0d01880 Mon Sep 17 00:00:00 2001 From: Sujal Gupta Date: Sat, 4 Oct 2025 12:15:28 +0530 Subject: [PATCH] Create binaryadd.cpp --- bit_manipulation/binaryadd.cpp | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 bit_manipulation/binaryadd.cpp diff --git a/bit_manipulation/binaryadd.cpp b/bit_manipulation/binaryadd.cpp new file mode 100644 index 0000000000..af4b4af50e --- /dev/null +++ b/bit_manipulation/binaryadd.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; + +class Solution { +public: + string addBinary(string a, string b) { + string result = ""; + int i = a.size() - 1, j = b.size() - 1, carry = 0; + + while (i >= 0 || j >= 0 || carry) { + int sum = carry; + + if (i >= 0) sum += a[i--] - '0'; + if (j >= 0) sum += b[j--] - '0'; + + result += (sum % 2) + '0'; + carry = sum / 2; + } + + reverse(result.begin(), result.end()); + return result; + } +}; + +int main() { + Solution solution; + + // Example 1 + string a1 = "11", b1 = "1"; + cout << "Input: a = \"11\", b = \"1\"" << endl; + cout << "Output: " << solution.addBinary(a1, b1) << endl << endl; + + // Example 2 + string a2 = "1010", b2 = "1011"; + cout << "Input: a = \"1010\", b = \"1011\"" << endl; + cout << "Output: " << solution.addBinary(a2, b2) << endl; + + return 0; +}