#!/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++; } #### 0 0000 0000 0000 0000 1 0000 0000 0000 1111 2 0000 0000 1111 0000 3 0000 0000 1111 1111 4 0000 1111 0000 0000 5 0000 1111 0000 1111 6 0000 1111 1111 0000 7 0000 1111 1111 1111 8 1111 0000 0000 0000 9 1111 0000 0000 1111 10 1111 0000 1111 0000 11 1111 0000 1111 1111 12 1111 1111 0000 0000 13 1111 1111 0000 1111 14 1111 1111 1111 0000 15 1111 1111 1111 1111