Add Binary

Easy#67bit by bit

You get two binary numbers written as strings of 0s and 1s, maybe of different lengths. Return their sum, also as a binary string. Our example: a = "1101" and b = "111" — the answer is "10100", that is 13 + 7 = 20.

input
a, b: two non-negative integers written in binary, as strings of 0 and 1.
returns
Their sum in binary, as a string, with no leading zeros: "101" + "11" is "1000".
constraints
  • 1 ≤ a.length, b.length ≤ 10⁴
  • a and b are made of 0 and 1, with no leading zeros except the number 0 itself
our 13 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {string} a
 * @param {string} b
 * @return {string}
 */
function addBinary(a, b) {

}
Ctrl ↵
format
reset

saved in this browser

13 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more bit manipulation problems

Pattern “Bit manipulation” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.