Rather late to the party but glob and pack could be another way to construct the array. Because each array element will be a multiple number of bytes long rather than nibbles (or should that be nybbles) odd widths will have a trailing null nybble in each element. The 0000 nybbles correspond to hex digit 0 and the 1111s to f so globing multiples of {0,f} will return all possible hex strings. We can then pack those using the H template. Using unpack with the B template and a count of 4 times the width in nybbles lets us see the bits inside each array element ignoring any trailing null nybble.
use strict; use warnings; use feature qw{ say }; my $sep = sub { say q{-} x 24; }; my $baseStr = q{{0,f}}; $sep->(); foreach my $width ( 3 .. 6 ) { say qq{Width - $width}; my $globStr = $baseStr x $width; my @bitStrs = map { pack q{H*}, $_ } glob $globStr; say unpack qq{B@{ [ $width * 4 ]}}, $_ for @bitStrs; say q{Element 2 - }, unpack qq{B@{ [ $width * 4 ]}}, $bitStrs[ 2 + ]; say q{Element 6 - }, unpack qq{B@{ [ $width * 4 ]}}, $bitStrs[ 6 + ]; say q{E. 2 & E. 6 - }, unpack qq{B@{ [ $width * 4 ]}}, $bitStrs[ 2 ] & $bitStrs[ 6 ]; $sep->(); }
The output.
------------------------ Width - 3 000000000000 000000001111 000011110000 000011111111 111100000000 111100001111 111111110000 111111111111 Element 2 - 000011110000 Element 6 - 111111110000 E. 2 & E. 6 - 000011110000 ------------------------ Width - 4 0000000000000000 0000000000001111 0000000011110000 0000000011111111 0000111100000000 0000111100001111 0000111111110000 0000111111111111 1111000000000000 1111000000001111 1111000011110000 1111000011111111 1111111100000000 1111111100001111 1111111111110000 1111111111111111 Element 2 - 0000000011110000 Element 6 - 0000111111110000 E. 2 & E. 6 - 0000000011110000 ------------------------ Width - 5 00000000000000000000 00000000000000001111 00000000000011110000 00000000000011111111 00000000111100000000 00000000111100001111 00000000111111110000 00000000111111111111 00001111000000000000 00001111000000001111 00001111000011110000 00001111000011111111 00001111111100000000 00001111111100001111 00001111111111110000 00001111111111111111 11110000000000000000 11110000000000001111 11110000000011110000 11110000000011111111 11110000111100000000 11110000111100001111 11110000111111110000 11110000111111111111 11111111000000000000 11111111000000001111 11111111000011110000 11111111000011111111 11111111111100000000 11111111111100001111 11111111111111110000 11111111111111111111 Element 2 - 00000000000011110000 Element 6 - 00000000111111110000 E. 2 & E. 6 - 00000000000011110000 ------------------------ Width - 6 000000000000000000000000 000000000000000000001111 000000000000000011110000 000000000000000011111111 000000000000111100000000 000000000000111100001111 000000000000111111110000 000000000000111111111111 000000001111000000000000 000000001111000000001111 000000001111000011110000 000000001111000011111111 000000001111111100000000 000000001111111100001111 000000001111111111110000 000000001111111111111111 000011110000000000000000 000011110000000000001111 000011110000000011110000 000011110000000011111111 000011110000111100000000 000011110000111100001111 000011110000111111110000 000011110000111111111111 000011111111000000000000 000011111111000000001111 000011111111000011110000 000011111111000011111111 000011111111111100000000 000011111111111100001111 000011111111111111110000 000011111111111111111111 111100000000000000000000 111100000000000000001111 111100000000000011110000 111100000000000011111111 111100000000111100000000 111100000000111100001111 111100000000111111110000 111100000000111111111111 111100001111000000000000 111100001111000000001111 111100001111000011110000 111100001111000011111111 111100001111111100000000 111100001111111100001111 111100001111111111110000 111100001111111111111111 111111110000000000000000 111111110000000000001111 111111110000000011110000 111111110000000011111111 111111110000111100000000 111111110000111100001111 111111110000111111110000 111111110000111111111111 111111111111000000000000 111111111111000000001111 111111111111000011110000 111111111111000011111111 111111111111111100000000 111111111111111100001111 111111111111111111110000 111111111111111111111111 Element 2 - 000000000000000011110000 Element 6 - 000000000000111111110000 E. 2 & E. 6 - 000000000000000011110000 ------------------------
I hope this is of interest.
Update: Clarified wording re. what to glob and inserted a blank line in code to separate generation from retrieval.
Cheers,
JohnGG
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Bit manipulation of a bit stream to generate different elements of an array with each nibble taking either 0 or 1 in perl
by stevieb (Canon) on Nov 20, 2018 at 01:19 UTC |