Just tell the formatter that you need to print exactly N (11) digits in any case, and that 0
should be used as a padding symbol:
my $num = 0b00000010001;
for (0..6) {
printf("%011b\n", $num);
$num <<= 1;
}
Demo.
Two sidenotes here
First, it's obviously redundant to do
print sprintf
:printf
will replace it quite well.Second, in the original code you forgot to assign the result of shifting back to
$num
. Had you started your script with theuse warnings
pragma (like in my demo), you'd have been notified about it withUseless use of left bitshift (<<) in void context...