|There's more than one way to do things|
|on Aug 24, 1999 at 22:42 UTC||Need Help??|
See the current Perl documentation for vec.
Here is our local, out-dated (pre-5.6) version:
vec - test or set particular bits in a string
Treats the string in EXPR as a vector of unsigned integers, and returns the value of the bit field specified by OFFSET. BITS specifies the number of bits that are reserved for each entry in the bit vector. This must be a power of two from 1 to 32. vec() may also be assigned to, in which case parentheses are needed to give the expression the correct precedence as in
vec($image, $max_x * $x + $y, 8) = 3;
Vectors created with vec() can also be manipulated with the logical operators
The following code will build up an
ASCII string saying
my $foo = ''; vec($foo, 0, 32) = 0x5065726C; # 'Perl' vec($foo, 2, 16) = 0x5065; # 'PerlPe' vec($foo, 3, 16) = 0x726C; # 'PerlPerl' vec($foo, 8, 8) = 0x50; # 'PerlPerlP' vec($foo, 9, 8) = 0x65; # 'PerlPerlPe' vec($foo, 20, 4) = 2; # 'PerlPerlPe' . "\x02" vec($foo, 21, 4) = 7; # 'PerlPerlPer' # 'r' is "\x72" vec($foo, 45, 2) = 3; # 'PerlPerlPer' . "\x0c" vec($foo, 93, 1) = 1; # 'PerlPerlPer' . "\x2c" vec($foo, 94, 1) = 1; # 'PerlPerlPerl' # 'l' is "\x6c"
To transform a bit vector into a string or array of 0's and 1's, use these:
$bits = unpack("b*", $vector); @bits = split(//, unpack("b*", $vector));
If you know the exact length in bits, it can be used in place of the