in reply to 5x6-bit values into/out of a 32-bit word
I'd do it this way. Encapsulate and abstractulate as appropriate:
my $num = shift; my $bits = 6; my $mask = (1 << $bits) - 1; my @out = map { ($num & $mask << $_ * $bits) >> $_ * $bits } 0..$bits-1;
update: oops, the map 0 .. $bits-1 is just a fortunate accident of 5 == 6-1. The following is better:
my $num = shift; my $groups = 5; my $bits = 6; my $mask = (1 << $bits) - 1; my @out = map { ($num & $mask << $_ * $bits) >> $_ * $bits } 0..$groups-1;
2nd update: oh, and you want to go back the other way. That's even easier:
my @in = (30, 14, 27, 2, 17); my $width = 6; my $offset = 0; my $out = 0; $out |= ($_ << ($offset++ * $width)) for @in;
• another intruder with the mooring in the heart of the Perl
|
|---|