For fun, I reproduced the following sequence:
https://oeis.org/A059893
using a crude tree process:
_________________________
#include <iostream>
#include <cmath>
namespace ct_rifc
{
unsigned long store(unsigned long x)
{
unsigned long i = 0;
unsigned long v = 0;
unsigned long tree_val = 0;
for (i = 0; v != x; ++i)
{
unsigned long bit = ((x >> i) % 2);
v += bit * std::pow(2, i);
unsigned long child_left = tree_val * 2 + 1;
unsigned long child_right = tree_val * 2 + 2;
if (bit == 0)
{
// left
//std::cout << "store left\n";
tree_val = child_left;
}
else
{
// right
//std::cout << "store right\n";
tree_val = child_right;
}
//std::cout << "bit = " << bit << "\n";
//std::cout << "v = " << v << "\n";
//std::cout << "tree_val = " << tree_val << "\n\n";
}
return tree_val / 2;
}
unsigned long load(unsigned long x)
{
x = x * 2;
unsigned long x_tmp = x;
unsigned long v = 0;
unsigned long levels = 0;
for (levels = 0; x_tmp != 0; ++levels)
{
x_tmp = (x_tmp - 1) / 2;
}
for (unsigned long i = 0; x != 0; ++i)
{
unsigned long parent = (x - 1) / 2;
unsigned long child_left = parent * 2 + 1;
unsigned long child_right = parent * 2 + 2;
if (x == child_left)
{
// std::cout << "load left\n";
//v += 0 * std::pow(2, levels - i - 1); // mul by zero,
nothing.
}
else
{
//std::cout << "load right\n";
v += 1 * std::pow(2, levels - i - 1);
}
x = parent;
}
return v;
}
void test()
{
for (unsigned long i = 0; i < 256; ++i)
{
unsigned long plaintext = i;
unsigned long ciphertext = store(plaintext);
unsigned long decrypted = load(ciphertext);
//std::cout << "plaintext = " << plaintext << "\n";
//std::cout << "ciphertext = " << ciphertext << "\n";
//std::cout << "decrypted = " << decrypted << "\n";
std::cout << "(pt, ct, dt) = " <<
plaintext << ", " << ciphertext << ", " << decrypted <<
"\n";
//std::cout << "\n";
if (plaintext != decrypted)
{
std::cout << "FAILURE!\n";
break;
}
}
}
}
int main()
{
{
ct_rifc::test();
std::cout << "\n\nFIN!\n";
}
//std::cin.get();
return 0;
}
_________________________
Can you compile and run this thing? Fwiw, here is a sample of the output:
(pt, ct, dt) = 0, 0, 0
(pt, ct, dt) = 1, 1, 1
(pt, ct, dt) = 2, 2, 2
(pt, ct, dt) = 3, 3, 3
(pt, ct, dt) = 4, 4, 4
(pt, ct, dt) = 5, 6, 5
(pt, ct, dt) = 6, 5, 6
(pt, ct, dt) = 7, 7, 7
(pt, ct, dt) = 8, 8, 8
(pt, ct, dt) = 9, 12, 9
(pt, ct, dt) = 10, 10, 10
(pt, ct, dt) = 11, 14, 11
(pt, ct, dt) = 12, 9, 12
(pt, ct, dt) = 13, 13, 13
(pt, ct, dt) = 14, 11, 14
(pt, ct, dt) = 15, 15, 15
(pt, ct, dt) = 16, 16, 16
(pt, ct, dt) = 17, 24, 17
(pt, ct, dt) = 18, 20, 18
(pt, ct, dt) = 19, 28, 19
(pt, ct, dt) = 20, 18, 20
Notice the pattern in the ct (middle) column:
0, 1, 2, 3, 4, 6, 5, 7, 8, 12, 10, 14, 9, 13, 11, 15, ...