Recursion:
use strict;
use warnings;
doShift (2, 5);
print "\n";
doShift (3, 6);
print "\n";
sub doShift {
my ($ones, $bits, $pattern, $limit) = @_;
--$ones;
$limit ||= $bits;
$pattern ||= 0;
for my $right ($ones .. $limit - 1) {
if ($ones) {
doShift ($ones, $bits, $pattern | (1 << $right), $right);
} else {
printf "%0*b\n", $bits, $pattern | (1 << $right);
}
}
}
Prints:
00011
00101
00110
01001
01010
01100
10001
10010
10100
11000
000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
DWIM is Perl's answer to Gödel
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.