ExamAdda Logo

Add Binary

Easy

Given two binary strings a and b, return their sum as a binary string.

A binary string contains only the characters 0 and 1.

Example 1

Input

a = "11"
b = "1"

Output

"100"

Explanation

   11
+ 01
   ----
  100

Example 2

Input

a = "1010"
b = "1011"

Output

"10101"

Explanation

   1010
+ 1011
    ------
   10101

Constraints

1 <= a.length
b.length <= 10^4
a and b consist only of '0' or '1'.
Each string does not contain leading zeros unless the string is "0".

Hints:

Hint 1

Start adding from the rightmost digits, just like normal decimal addition.

Hint 2

Keep a carry variable.

For each position:

sum = bitA + bitB + carry

 

The current binary digit is:

sum % 2

 

and the new carry is:

sum / 2

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 22, 2026

Last updated

Aug 23, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output