in reply to using pack() to build a 16 bit word

To pack using bitstrings, you have to have bitstrings (strings of 1s and 0s), not decimal numbers. You can get bitstrings from sprintf:
my ($pos, $aw, $stru) = (2, 5, 6); my $bits = sprintf('%06b%05b%03b%02b', 0, $stru, $aw, $pos); print "Bits=<$bits>\n"; my $bstr=pack('b16', $bits); printf "<$bstr> is %d bytes\n", length($bstr); print "\nUnpack: ", unpack('B*', $bstr), "\n";
If you use format strings like 'b6 b5 b3 b2', each unit will be padded out to full byte width, rather than being all jammed together.

The PerlMonk tr/// Advocate

Replies are listed 'Best First'.
Re: Re: using pack() to build a 16 bit word
by fizbin (Chaplain) on May 12, 2004 at 12:33 UTC
    Note that whitespace in bitstrings is irrelevant, so if it makes your debugging easier to do, you can always insert some spaces in that sprintf line.
    -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/