Skip to main content

Add Binary

Given two binary strings a and b Return their sum as a binary string Example: a = "11", b = "1" => "100" Example: a = "1010", b = "1011" => "10101"

Given two non-empty binary strings a and b, return their sum as a binary string. Each input contains only 0 and 1 and has no leading zeros unless it is "0". The output must also have no leading zeros, with "0" representing a zero sum.

StringMath & GeometryBit ManipulationSimulation

Examples

You write

add_binary(a: str, b: str)

  1. Example 1

    Input
    a = "11", b = "1"
    returns
    "100"
  2. Example 2

    Input
    a = "1010", b = "1011"
    returns
    "10101"

The pattern behind it

Strings

I reach for it when

  • The input is text and the answer is a count, a substring or a new string.
  • You need to compare, reverse or rebuild characters.

Say this before you type. Say which array pattern the string is hiding before you touch a character.

All 51 Strings problems

Bit Manipulation

I reach for it when

  • The statement bans extra space or restricts operators.
  • Numbers act as flags, masks or XOR pairs.

Say this before you type. Say what each bit means in this problem before you shift or mask anything.

All 12 Bit Manipulation problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Add Two Binary Stringseasy