Add Binary
Easy Bit Manipulation
a and b are two whole numbers, neither negative, written in binary: as strings of the digits 0 and 1, the most significant digit first.
Return their sum, written the same way in binary, with no 0 in front unless the sum is 0.
Example 1
Input: a = "1101", b = "111"
Output: "10100"
In tens these are 13 + 7 = 20. Column by column from the right: 1 + 1 is 10, so 0 and carry 1; 0 + 1 + 1 is 10 again; 1 + 1 + 1 is 11, so 1 and carry 1; 1 + 1 is 10; the last carry makes 10100.
Example 2
Input: a = "0", b = "1001"
Output: "1001"
Example 3
Input: a = "1111", b = "1111"
Output: "11110"
Constraints
1 ≤ a.length, b.length ≤ 2 × 10⁴aandbhold only the characters0and1- Neither has a 0 in front, but the number 0 itself, written
0
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
See the AI roundMore Bit Manipulation
- Single NumberAI round · Very easy
- Single Number IIAI round · Medium
- Single Number IIIAI round · Medium
- Power of TwoEasy
- Number of 1 BitsEasy