Add two bit strings (original) (raw)
Last Updated : 26 Apr, 2025
Given two binary strings s1 and **s2 consisting of only 0s and 1s. Find the resultant string after adding the two Binary Strings.
**Note: The input strings may contain leading zeros but the output string should not have any leading zeros.
**Examples:
**Input: s1 = "1101", s2 = "111"
**Output: 10100
**Explanation: "1101" and "111" in decimal representation will be 13 and 7 respectively. Adding both the numbers gives 20, and its binary representation is "10100".
**Input: s1 = "00100", s2 = "010"
**Output: 110
**Explanation: "00100" and "010" in decimal representation will be 4 and 2 respectively. Adding both the numbers gives 6, and its binary representation is "110".
This problem can be solved using Bit-by-bit addition with carry in O(n + m) Time and O(1) Space, The idea is to first trim the leading zeros in the input strings. Now, start from the **last characters of the strings and compute the digit sum one by one. If the **sum becomes more than **1, then store **carry for the next digits. Also consider this **carry while calculating the digit sum. After calculating the sum, if an additional carry is generated, prepend a ‘1’ of the **result.
Please refer Add two binary strings for detailed explanation and solution.
Similar Reads
- Bitwise Algorithms Bitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit 4 min read
- Introduction to Bitwise Algorithms - Data Structures and Algorithms Tutorial Bit stands for binary digit. A bit is the basic unit of information and can only have one of two possible values that is 0 or 1. In our world, we usually with numbers using the decimal base. In other words. we use the digit 0 to 9 However, there are other number representations that can be quite use 15+ min read
- Bitwise Operators in C In C, bitwise operators are used to perform operations directly on the binary representations of numbers. These operators work by manipulating individual bits (0s and 1s) in a number.The following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are 6 min read
- Bitwise Operators in Java In Java, Operators are special symbols that perform specific operations on one or more than one operands. They build the foundation for any type of calculation or logic in programming.There are so many operators in Java, among all, bitwise operators are used to perform operations at the bit level. T 6 min read
- Python Bitwise Operators Python bitwise operators are used to perform bitwise calculations on integers. The integers are first converted into binary and then operations are performed on each bit or corresponding pair of bits, hence the name bitwise operators. The result is then returned in decimal format.Note: Python bitwis 5 min read
- JavaScript Bitwise Operators In JavaScript, a number is stored as a 64-bit floating-point number but bitwise operations are performed on a 32-bit binary number. To perform a bit-operation, JavaScript converts the number into a 32-bit binary number (signed) and performs the operation and converts back the result to a 64-bit numb 5 min read
- All about Bit Manipulation Bit Manipulation is a technique used in a variety of problems to get the solution in an optimized way. This technique is very effective from a Competitive Programming point of view. It is all about Bitwise Operators which directly works upon binary numbers or bits of numbers that help the implementa 14 min read
- What is Endianness? Big-Endian & Little-Endian Computers operate using binary code, a language made up of 0s and 1s. This binary code forms the foundation of all computer operations, enabling everything from rendering videos to processing complex algorithms. A single bit is a 0 or a 1, and eight bits make up a byte. While some data, such as cert 5 min read
- Bits manipulation (Important tactics) Prerequisites: Bitwise operators in C, Bitwise Hacks for Competitive Programming, Bit Tricks for Competitive Programming Table of Contents Compute XOR from 1 to n (direct method)Count of numbers (x) smaller than or equal to n such that n+x = n^xHow to know if a number is a power of 2?Find XOR of all 15+ min read