Simplified the solution slightly
Fully working for all (2-, 3- and 8-bit) patterns.
Polished test bench, added schematic for N == 4