#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <climits>
static void printBits(size_t bits)
{
size_t tip = CHAR_BIT * sizeof(size_t);
std::printf("%zu -> ", bits);
while (tip--) {
std::putc( ((bits >> tip) & 1) ? '1' : '0' , stdout);
}
std::putc('\n', stdout);
}
struct BitRule {
BitRule(unsigned char check, unsigned char write): _check(check), _write(write) {}
unsigned char _check;
unsigned char _write;
};
#define CHECK_PUTB(cond) \
if (cond) \
put1(); \
else \
put0()
class Bit8Writer {
public:
Bit8Writer(unsigned char start = 0): _state(0), _bits(start)
{}
operator unsigned char () const { return _bits;}
void reset(unsigned char bits) {
_bits = bits;
}
void inline put0() {
_bits &= ~(1 << _state);
adv();
}
void inline put1() {
_bits |= (1 << _state);
adv();
}
void put(unsigned char b) {
CHECK_PUTB(b & 1);
CHECK_PUTB((b >> 1) & 1);
CHECK_PUTB((b >> 2) & 1);
CHECK_PUTB((b >> 3) & 1);
CHECK_PUTB((b >> 4) & 1);
CHECK_PUTB((b >> 5) & 1);
CHECK_PUTB((b >> 6) & 1);
CHECK_PUTB((b >> 7) & 1);
}
void apply(const unsigned char target, const BitRule& rule) {
if (target & rule._check)
put(rule._write);
}
private:
void inline adv() { _state = _state == CHAR_BIT ? 0 : _state + 1;}
size_t _state;
unsigned char _bits;
};
#undef CHECK_PUTB
static const BitRule RULES[] = {
BitRule(41, 101),
BitRule(201, 94),
BitRule(255, 35),
BitRule(9, 145),
BitRule(93, 192),
BitRule(55, 44),
BitRule(214, 5),
BitRule(78, 124),
BitRule(220, 62),
BitRule(101, 191)
};
static const size_t RULE_SIZE = sizeof(RULES) / sizeof(BitRule);
static uint8_t runRules(uint8_t start) {
static Bit8Writer wt8;
size_t i = RULE_SIZE;
while (i--) {
wt8.apply(start, RULES[i]);
}
return (unsigned char)wt8;
}
int main(int argc, char const* argv[]) {
size_t j = 10;
uint8_t cell = 0x0e;
printBits(cell);
while (j--) {
cell = runRules(cell);
printBits(cell);
}
return 0;
}