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
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;
}

C++相关代码片段

how to kill

hello world cpp

cpp hello world

when kotlin

how to write hello world c++

fenwick tree

main function

python loop

is palindrom

subsequence

insertion sort

react cookie

data structure

Stack Modified

increment integer

496. Next Greater Element I.cpp

c++ freecodecamp course 10 hours youtube

simple interest rate

deliberation meaning

fingerprint

c++ system() from variable

operator using

unambiguous

Stream Overloading

quick_sort

hash table

graph colouring

the question for me

fname from FString

how togreper

is plaindrome

logisch oder

robot framework for loop example

spyder enviroment

pallindrome string

ue4 c++ string from fvector

interactive problem

two sum solution

interpolation search

Required Length

new expression

Targon lol

xor linked list

stack implementation

typescript

loop through array

loop array

how to point to next array using pointer c++

file exist

floating point exception

array di struct

castin in C++

varint index

how to make a instagram report bot python

windows servis from console app

add integers

listas enlazadas/ linked lists

niet werkend

bubble sort

triangle angle sum

divisor summation

rotateArray

destiny child

Populating Next Right Pointers in Each Node

cosnt cast

bucket sort

double plus overload

Z-function

binary search

permutation

linked list

Implicit conversion casting

square root

public method

tower of hanoi

selection sort

dangling pointer

hello world programming

statements

volumeof a sphere