rsiedl has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I've got a simple question as I am still just learning.
I am trying to understand the following block of code...
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

The area I am having trouble understanding is the
if ($i & 2**$j) { } # end-if

Is this saying
if ($i == 2**$j) { } # end-if
Any help would be appreciated.

Cheers,
Reagen

Update: Title =~ s/for loops/if conditions/

Replies are listed 'Best First'.
Re: understanding for loops
by BrowserUk (Patriarch) on Nov 21, 2004 at 11:10 UTC

    '&' is bit-wise boolean and. The expression ($i & 2**$j), is testing the binary value of $i and returns true if the $jth bit is set to one.

    This may be clarified by this slightly modified version of your example code:

    [11:04:18.32] P:\test>perl my $n = 4; for my $i (1..2**$n-1) { printf "%2d (0b%04b) has bits [ ", $i, $i; for my $j (0..$n-1) { if ($i & 2**$j) { print $j, " "; } # end-if } # end-for print "] set\n"; } # end-for ^Z 1 (0b0001) has bits [ 0 ] set 2 (0b0010) has bits [ 1 ] set 3 (0b0011) has bits [ 0 1 ] set 4 (0b0100) has bits [ 2 ] set 5 (0b0101) has bits [ 0 2 ] set 6 (0b0110) has bits [ 1 2 ] set 7 (0b0111) has bits [ 0 1 2 ] set 8 (0b1000) has bits [ 3 ] set 9 (0b1001) has bits [ 0 3 ] set 10 (0b1010) has bits [ 1 3 ] set 11 (0b1011) has bits [ 0 1 3 ] set 12 (0b1100) has bits [ 2 3 ] set 13 (0b1101) has bits [ 0 2 3 ] set 14 (0b1110) has bits [ 1 2 3 ] set 15 (0b1111) has bits [ 0 1 2 3 ] set

    The first column is the value of $i in decimal. The second is the value of $i as binary. The numbers in []s are the positions (numbered right to left, from zero) of the bits that are set in the current value of $i.


    Examine what is said, not who speaks.
    "But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
    "Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      Thanks BrowserUk!
[DONE ;-)]
by Luca Benini (Scribe) on Nov 21, 2004 at 11:33 UTC
    & is the bit bit operator == is the logical operator for compare The first if check if the bit of position $j was setted in $i. The second checsk if $i is 2**$j
Re: understanding if conditions
by pg (Canon) on Nov 21, 2004 at 18:20 UTC

    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
A reply falls below the community's threshold of quality. You may see it by logging in.