Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.4 kB
1
Indexable
Never
#include <bits/stdc++.h>
using namespace std;

int main()
{
    string data = "101011001";
    // cin>>data;
    string divisor = "1010";
    // cin>>divisor;

    // appending no. of bits - 1 to data;
    int leng = divisor.length() - 1;
    string test =data;

    while (leng-->0)
    {
        test += '0';
    }

    cout << "Padded data: " << test << endl;
    // logic for xor
    int count = 0;
    string res;
    bool flag = true;
    cout<<"test data: ";
    while (test.length() > divisor.length())
    {
        string d = test.substr(count, count + divisor.length());
        res = "";

        for (int i = 0; i < divisor.length(); ++i)
        {
            if (flag)
            {
                if (d[i] != divisor[i])
                {
                    res += '1';
                    flag = false;
                }
            }
            else
            {
                if (d[i] != divisor[i])
                {
                    res += '1';
                }
                else
                {
                    res += '0';
                }
            }
        }
        count += divisor.length();
        test = res + test.substr(count, test.length());
        cout<<test<<endl;
        flag = true;
    }
    cout << endl
         << test;
    return 0;
}