in reply to all binary combinations

$ perl -le' my $length = 3; for ( my $i; ; ++$i ) { last if $i & 1 << $length; printf "%0*b\n", $length, $i; } ' 000 001 010 011 100 101 110 111

Replies are listed 'Best First'.
Re^2: all binary combinations
by perlrocks (Acolyte) on Sep 05, 2009 at 15:26 UTC

    While testing your code, i realized that when length >= 32, the number of results is incorrect. Do you have any idea why?

    Thanks a lot,

    Hadi

Re^2: all binary combinations
by perlrocks (Acolyte) on Sep 04, 2009 at 15:46 UTC

    Thanks. But i'm getting the following warning.

    Use of uninitialized value $i in bitwise and (&) at C:\Users\Hadi\Desktop\Academics\Research\0-recombination\testing1.pl line 6.

    Is there a way i can avoid it. Thanks

      initializing $i=0 worked fine :)