my $n = 4; for my $i (1..2**$n-1) { for my $j (0..$n-1) { if ($i & 2**$j) { print $j, "\n"; } # end-if } # end-for } # end-for