#!/usr/bin/perl # author: bliako # date: 16/11/2018 # https://perlmonks.org/?node_id=1225889 # for given width, it counts from 0 to 2**W # and prints the count in binary. # It misses logic to discard some nibbles use strict; use warnings; my $W = 4; # width in nibbles my $count = 0; my $stop = 2**$W; # format an integer as binary my $format = "%0.${W}b"; while( $count < $stop ){ my $x = sprintf($format, $count); # x now contains a binary number of the count # between 0 and the stop 2^width_in_nibbles # now make each digit a nibble, e.g 0 -> 0000 $x =~ s/0/a/g; $x =~ s/1/b/g; $x =~ s/a/0000 /g; $x =~ s/b/1111 /g; print "x=$x ($count)\n"; $count++; }