Search
 
SCRIPT & CODE EXAMPLE
 

CPP

binary string addition

//** fastest solution possible **//
#include <string_view>
#define int int_least8_t
class Solution {
public:
    string addBinary(const string_view& a, const string_view& b) {
        bool carry = 0;
        const string_view* small;
        const string_view* large;
        if(a.size() > b.size()) {
            small = &b;
            large = &a;
        }
        else {
            small = &a;
            large = &b;
        }
        
        string ans;
      	ans.reserve (a.size());
        int itrL = large[0].size() - 1;
        for(int i = small[0].size() - 1; i >= 0; i--) {
            if(small[0][i] == '1' && large[0][itrL] == '1' && carry == 1) {
                ans += "1";
                carry = 1;
            }
            else if(small[0][i] == '1' && large[0][itrL] == '1' && carry == 0) {
                ans += "0";
                carry = 1;
            }
            else if((small[0][i] == '1' && large[0][itrL] == '0' && carry == 0) 
                 || (small[0][i] == '0' && large[0][itrL] == '1' && carry == 0) ) {
                ans += "1";
                carry = 0;
            }
            else if(small[0][i] == '0' && large[0][itrL] == '0' && carry == 1){
                ans += "1";
                carry = 0;
            }
            else if(small[0][i] == '0' && large[0][itrL] == '0' && carry == 0){
                ans += "0";
                carry = 0;
            }
            else {
                ans += "0";
                carry = 1;
            }
            itrL--;
        }
        
        while(itrL >= 0){
             if(large[0][itrL] == '1' && carry == 1){
                ans+="0";
                carry=1;
            }
            else if((large[0][itrL] == '1' && carry == 0)
                 || (large[0][itrL] == '0' && carry == 1)){
                ans += "1";
                carry = 0;
            }
            else{
                ans += "0";
                carry = 0;
            }
            itrL--;
        }
        
        if(carry == 1){
            ans += "1";
        }
        
        reverse(ans.begin(), ans.end());
        return ans;
    }
};
#undef int
Comment

binary add using strings

#include <iostream>
#include <string>
using namespace std;

int DecToBin(int dec)
{
    long long bin = 0;
    int j,rem, i = 1;

    for(j=dec;j!=0;j)
    {
        rem = j % 2;
        j /= 2;
        bin += rem * i;
        i *= 10;
    }

    return bin;
}

int main()
{
    string number_1, number_2;
    int decimal_sum;

    cout<< "Enter first number: ";
    cin>> number_1;

    cout<< "Enter second number: ";
    cin>> number_2;

    decimal_sum = stoi(number_1 , 0 ,2) + stoi(number_2 , 0 ,2);

    cout<< "Binary Sum = " <<DecToBin(decimal_sum);

    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: swap values in array c++ 
Cpp :: iterate vector from end to begin 
Cpp :: how to change string to lowercase and uperCase in c++ 
Cpp :: initialize 2d array c++ memset 
Cpp :: what is _asm in C++ 
Cpp :: loop through char in string c++ 
Cpp :: how to convert character to lowercase c++ 
Cpp :: array 2d dynamic allocation c++ 
Cpp :: string input with space c++ stl 
Cpp :: c++ code for insertion sort 
Cpp :: c++ check if string is empty 
Cpp :: how to use string variable in switch case in c++ 
Cpp :: return by reference in cpp 
Cpp :: change abstract title name latex 
Cpp :: iff arduino 
Cpp :: c++ declare variable 
Cpp :: iterating in map/unordered map c++ 
Cpp :: string to vector char c++ 
Cpp :: c++ std::sort 
Cpp :: delete one specific character in string C++ 
Cpp :: find in string c++ 
Cpp :: check if character is uppercase c++ 
Cpp :: Story of c++ 
Cpp :: set was not declared in this scope 
Cpp :: c++ get character from string 
Cpp :: c++ friend class 
Cpp :: string to uint64_t c++ 
Cpp :: std::iomanip c++ 
Cpp :: c++ modulo positive 
Cpp :: modulo subtraction 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =