Reputation: 389
I am writing a perl script which will generate string output of 0s and 1s.
I have numbers from 2 to 27. I want to convert them to binary number of fixed length 5 and then reverse the order.
For example, if I convert 2 to binary, it will be 00010
(fixed length of 5) and then I would like to get it reversed such that output is 01000
.
I am iterating the numbers through a for loop in perl.
What will be a sweet one liner or 2 to do this. Any bash or perl one liner or 2 that I can use in my perl script.
I am able to do it in 2 lines though but still looking for any one liner in bash.
for (my $i=02; $i <= 27; $i++) {
my $j = sprintf ("%05b\n", $i);
my $k = reverse $j;
}
Upvotes: 0
Views: 205
Reputation: 40768
Try this:
say for map { scalar reverse sprintf "%05b", $_ } 2..27;
Output:
01000
11000
00100
10100
01100
11100
00010
10010
01010
11010
00110
10110
01110
11110
00001
10001
01001
11001
00101
10101
01101
11101
00011
10011
01011
11011
Upvotes: 1