my $n = 4; for my $i (1..2**$n-1) { my $str = ""; for my $j (0..$n-1) { $str = (($i & 2**$j) ? '1' : '0') .$str; } print "$i = $str\n" }