in reply to understanding if conditions
A slight change to the code reveals its purpose:
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" }
This prints:
1 = 0001 2 = 0010 3 = 0011 4 = 0100 5 = 0101 6 = 0110 7 = 0111 8 = 1000 9 = 1001 10 = 1010 11 = 1011 12 = 1100 13 = 1101 14 = 1110 15 = 1111
|
|---|