Skip to content

Conversation

Sujalgupta2p
Copy link

feat: Add Binary addition algorithm in C++

This program adds two binary numbers given as strings and returns their sum as a binary string.

About

This algorithm solves the problem of adding two binary numbers without using built-in base conversion functions.
It is based on LeetCode Problem 67 — Add Binary.

How it works

  1. Traverse both strings from right to left (least significant bit first).
  2. Add corresponding bits plus the carry.
  3. Append (sum % 2) to the result and update carry = sum / 2.
  4. Reverse the final result to get the correct binary sum.

Example

Input: a = "1010", b = "1011"
Output: "10101"

Reference

https://en.wikipedia.org/wiki/Binary_arithmetic

✅ Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work — I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file.
  • All new C++ files are placed inside an existing directory.
  • All filenames are lowercase characters with underscores (no spaces or dashes).
  • All functions and variable names follow C++ naming conventions (snake_case or camelCase).
  • All function parameters and return values are clearly defined.
  • All new algorithms include at least one URL that points to Wikipedia or another explanation.

this program adds two binary numbers given as strings and returns their sum as a binary string.
@Sujalgupta2p
Copy link
Author

Hi! My PR adds a binary addition algorithm in C++.
Could someone please review it and approve the workflow so it can be merged?
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants