Skip to content

Add Binary

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

You may assume neither string has leading zeros, except for the zero itself.

Input aInput bOutputExplanation
"11""1""100"3 + 1 = 4 in binary
"1010""1011""10101"10 + 11 = 21 in binary
"0""0""0"Edge case: zero + zero
  • 1 <= a.length, b.length <= 10^4
  • a and b consist of only '0' or '1' characters
  • Each string does not have leading zeros except for the zero itself
ApproachTimeSpaceNotes
String SimulationO(max(n, m))O(max(n, m))Manual carry propagation, easy to understand
Bit OperationsO(max(n, m))O(max(n, m))Convert to integers, add natively, convert back

* n = length of string a, m = length of string b

  • Pick String Simulation if you want to understand how binary addition works at the bit level.
  • Pick Bit Operations if you want the simplest implementation using built-in radix conversion.
Most Intuitive
String Simulation
O(max(n, m)) time · O(max(n, m)) space
Simplest Code
Bit Operations
O(max(n, m)) time · O(max(n, m)) space
Section titled “Approach 1: String Simulation (Recommended)”
★ Recommended

Simulate elementary school binary addition: process digits from right to left, maintaining a carry bit. When you reach the end of the shorter string, pad with zeros and continue.

⏱ Time O(max(n, m)) Single pass right to left 💾 Space O(max(n, m)) Result string storage

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

Stepija[i]b[j]carrysumAction
1330101Append 1, carry=0
2221102Append 0, carry=1
3110011Append 1, carry=0
4001102Append 0, carry=1
5-1-1--1-Append 1, done
Result: "10101"
1010 (input a)
+ 1011 (input b)
------
Step 1: 0 + 1 + 0 = 1, no carry
Step 2: 1 + 1 + 0 = 10 (binary) = 0 with carry 1
Step 3: 0 + 0 + 1 = 1, no carry
Step 4: 1 + 1 + 0 = 10 (binary) = 0 with carry 1
Step 5: carry 1 left = 1
Result: 10101
function addBinaryStringSimulation(a, b):
result = []
carry = 0
i = len(a) - 1
j = len(b) - 1
while i >= 0 or j >= 0 or carry:
digitA = a[i] if i >= 0 else 0
digitB = b[j] if j >= 0 else 0
total = digitA + digitB + carry
result.append(total % 2)
carry = total // 2
i -= 1
j -= 1
return reverse(result).join('')
add_binary_string_simulation.py
def add_binary_string_simulation(a: str, b: str) -> str:
"""
String simulation approach - simulate binary addition from right to left.
Time: O(max(len(a), len(b)))
Space: O(max(len(a), len(b))) for result
"""
result = []
carry = 0
i = len(a) - 1
j = len(b) - 1
while i >= 0 or j >= 0 or carry:
digit_a = int(a[i]) if i >= 0 else 0
digit_b = int(b[j]) if j >= 0 else 0
total = digit_a + digit_b + carry
result.append(str(total % 2))
carry = total // 2
i -= 1
j -= 1
return ''.join(reversed(result))
print(add_binary_string_simulation("11", "1")) # "100"
print(add_binary_string_simulation("1010", "1011")) # "10101"
print(add_binary_string_simulation("0", "0")) # "0"
✓ Simple

Convert both binary strings to integers using the built-in radix parser, add them using native arithmetic, then convert the result back to binary.

This approach leverages language-native support for arbitrary precision (Python, JavaScript BigInt) or fixed-width integers with overflow-safe addition.

⏱ Time O(max(n, m)) Conversion + addition 💾 Space O(max(n, m)) Result string storage

Input: a = "11", b = "1"

Step 1: int('11', 2) = 3
Step 2: int('1', 2) = 1
Step 3: 3 + 1 = 4
Step 4: bin(4) = '0b100'
Result: '100' (strip '0b' prefix)
function addBinaryBitOperations(a, b):
numA = parseInt(a, 2)
numB = parseInt(b, 2)
total = numA + numB
return bin(total).replace('0b', '')
add_binary_bit_operations.py
def add_binary_bit_operations(a: str, b: str) -> str:
"""
Bit operations approach - convert to int, add, convert back to binary.
Time: O(max(len(a), len(b)))
Space: O(max(len(a), len(b))) for result
"""
num_a = int(a, 2) # Convert binary string to integer
num_b = int(b, 2)
total = num_a + num_b
return bin(total)[2:] # Convert back to binary string (remove '0b' prefix)
print(add_binary_bit_operations("11", "1")) # "100"
print(add_binary_bit_operations("1010", "1011")) # "10101"
print(add_binary_bit_operations("0", "0")) # "0"
✓ Simple

A third approach demonstrating an alternative technique for solving this problem. This implementation optimizes either time or space complexity compared to the previous approaches.

⏱ Time O(n) Optimized complexity 💾 Space O(1) Efficient space usage
function approach3():
// Implementation approach goes here
add_binary_space_optimized.py
"""
Solution for Add Binary
"""
def solve():
"""Implementation for approach 3 of Add Binary"""
pass
if __name__ == "__main__":
print("Solution ready")