vec 
vecEXPR,OFFSET,BITS
The vec function provides compact storage of lists of unsigned integers.
These integers are packed as tightly as possible within an ordinary Perl
string. The string in EXPR is treated as a
bit string made up of some arbitrary number of elements, depending on
the length of the string.
OFFSET specifies the index of the
particular element you’re interested in. The syntaxes for reading and
writing the element are the same, since vec stores or returns the value of the element
depending on whether you use it in an lvalue or an rvalue
context.
BITS specifies how wide each element is
in bits, which must be a power of two: 1, 2,
4, 8, 16, or
32 (and also 64 on some platforms). (An exception is raised
if any other value is used.) Each element can therefore contain an
integer in the range 0..(2.
For the smaller sizes, as many elements as possible are packed into each
byte. When BITS)–1BITS is 1, there are eight elements per byte. When
BITS is 2,
there are four elements per byte. When BITS
is 4, there are two elements
(traditionally called nybbles) per byte. And so on. Integers larger than
a byte are stored in big-endian order.
A list of unsigned integers can be stored in a single scalar
variable by assigning them individually to the vec function. (If
EXPR is not a valid lvalue, an exception is
raised.) In the following example, the elements are each 4 bits
wide:
$bitstring ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access