Encryption

// Source -> Encrypted
// 10 -> 11 (1 + 0) (1 + 0)
// 1010 -> 1211 (1 + 0) (1 + 0 + 1) (0 + 1 + 0) (1 + 0)
//
// 1010
// |/
// 1... (1+0)

// 1010
// \|/
// .2.. (1+0+1)

// 1010
//  \|/
// ..1. (0+1+0)

// 1010
//   \|
// ...1 (1+0)

// Result: 1211

// Write function to go from encrypted to source
// Source numbers are always 1 or 0

Using Just Strings

// assuming string sequence - iterative solution
string next_n(const string &str, const unsigned pos, const unsigned n)
{
    unsigned short sum = 0, go_to = pos + n;
    for (int i = pos; i < go_to; i++)
        sum += (str[i] - '0');
    return to_string(sum);
}

string num_src_to_encryptd(const string &bits)
{
    if (bits.length() <= 1) return bits;
    string encrypted = "";
    encrypted.reserve(bits.length());

    encrypted += next_n(bits, 0, 2);
    for (int i = 0; i < bits.length() - 2; i++)
        encrypted += next_n(bits, i, 3);

    encrypted += next_n(bits, bits.length() - 2, 2);

    return encrypted;
}

int main()
{
    // string test
    string test0 = "1";
    cout << num_src_to_encryptd(test0) << endl;

    string test1 = "10";
    cout << num_src_to_encryptd(test1) << endl;

    string test2 = "1010";
    cout << num_src_to_encryptd(test2) << endl;

    string test3 = "10101100";
    cout << num_src_to_encryptd(test3) << endl;
}

Strings plus some Math

// assuming bit sequence = iterative solution
unsigned short num_at_MSD(const int num_digits, const int num, const int i) {
    return int(num / pow(10, num_digits - 1 - i)) % 10;
}

unsigned short num_digits(int num)
{
    int div = 1;
    int count = 0;
    while (div <= num) {
        div *= 10;
        count++;
    }
    return count;
}

string num_src_to_encryptd(const int num)
{
    string bit_seq = "";
    int size = num_digits(num), sum = 0;
    if (size <= 1) return to_string(num);


    bit_seq += to_string(num_at_MSD(size, num, 0) +
        num_at_MSD(size, num, 1));
    for (int i = 0; i < size - 2; i++) {
        for (int j = 0; j < 3; j++) {
            sum += num_at_MSD(size, num, i + j);
        }
        bit_seq += to_string(sum);
        sum = 0;
    }
    bit_seq += to_string(num_at_MSD(size, num, size - 2) +
        num_at_MSD(size, num, size - 1));

    return bit_seq;
}


int main()
{
    int test0 = 1; 
    cout << num_src_to_encryptd(test0) << endl;

    int test1 = 10; 
    cout << num_src_to_encryptd(test1) << endl;

    int test2 = 1010; 
    cout << num_src_to_encryptd(test2) << endl;

    int test3 = 10101100; 
    cout << num_src_to_encryptd(test3) << endl;
}

Bitwise

unsigned msb(int num)
{
    unsigned count = 0;
    while (num >>= 1)
        count++;
    return count;
}

bool getBit(int num, int n) {
    return (num & (1 << n)) > 1;
}


int num_src_to_encryptd(const int num)
{
    int bit_seq = 0;
    int multiplier = 1;
    int size = msb(num) + 1, sum = 0;

    if (size <= 1) return num;
    bit_seq += (getBit(num, 0) + getBit(num, 1)) * multiplier;
    multiplier *= 10;

    for (int i = 0; i < size - 2; i++) {
        for (int j = 0; j < 3; j++) {
            sum += getBit(num, i + j) * multiplier;
        }
        bit_seq += sum;

        multiplier *= 10;
        sum = 0;
    }
    bit_seq += (getBit(num, size - 1) + getBit(num, size - 2)) * multiplier;

    return bit_seq;
}


int main()
{
    int test0 = 1; // 1
    cout << num_src_to_encryptd(test0) << endl;

    int test1 = 2; // 10
    cout << num_src_to_encryptd(test1) << endl;

    int test2 = 10; // 1010
    cout << num_src_to_encryptd(test2) << endl;

    int test3 = 172; // 10101100
    cout << num_src_to_encryptd(test3) << endl;
}

Last updated