Add Binary - Problem

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

A binary string contains only characters '0' and '1'. You need to perform binary addition following the same rules as decimal addition, but in base 2.

Binary Addition Rules:

  • 0 + 0 = 0
  • 0 + 1 = 1
  • 1 + 0 = 1
  • 1 + 1 = 10 (0 with carry 1)

Input & Output

Example 1 — Basic Addition
$ Input: a = "11", b = "1"
Output: "100"
💡 Note: Binary addition: 11₂ + 1₂ = 100₂ (which is 3 + 1 = 4 in decimal)
Example 2 — Different Lengths
$ Input: a = "1010", b = "1011"
Output: "10101"
💡 Note: Binary addition: 1010₂ + 1011₂ = 10101₂ (which is 10 + 11 = 21 in decimal)
Example 3 — Single Digit
$ Input: a = "1", b = "1"
Output: "10"
💡 Note: Binary addition: 1₂ + 1₂ = 10₂ (which is 1 + 1 = 2 in decimal)

Constraints

  • 1 ≤ a.length, b.length ≤ 104
  • a and b consist only of '0' or '1' characters
  • Each string does not contain leading zeros except for the zero itself

Visualization

Tap to expand
Add Binary: "1011" + "1" = "1100""1011"Binary String A"1"Binary String BBinary AdditionBit-by-bit from rightwith carry propagation"1100"Result StringProcess: Start from rightmost bits, add with carry, build result left✓ Works for any length binary strings without integer overflow
Understanding the Visualization
1
Input
Two binary strings: "1011" and "1"
2
Process
Add bit by bit from right to left with carry
3
Output
Result binary string: "1100"
Key Takeaway
🎯 Key Insight: Binary addition follows the same carry logic as decimal, but carries occur when sum ≥ 2
Asked in
Facebook 15 Google 12 Amazon 8
89.5K Views
Medium Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen