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 :: how to define range of numbers inside a if condition in c++ 
Cpp :: cpp hello world 
Cpp :: c++ error 0xC0000005 
Cpp :: c++ pwstr to char* 
Cpp :: 2927260.eps 2927262.jpg 2927263.ai License free.txt License premium.txt 
Cpp :: is the c++ 20 char te same as the old one 
C :: trie tableau c 
C :: c bold text 
C :: swapping of two numbers in c without temporary variable 
C :: c get time in milliseconds 
C :: factorial c program using for loop 
C :: c program hide console window 
C :: grepper vscode 
C :: come creare variabili casuali in c 
C :: add border to image android 
C :: first program in c 
C :: #![feature]` may not be used on the // stable release channel 
C :: que es % en c 
C :: servo motor arduino 
C :: function for quicksort in c 
C :: malloc in c 
C :: mongodb update 
C :: how to print sizes of various data types of C 
C :: Convert mpg / mp4 to gif with ffmpeg 
C :: isspace 
C :: strings in c 
C :: typedef vs #define 
C :: bubble sort 
C :: mongo connect db 
C :: how to run c program from visual studio terminal 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =