Perl Programming/Keywords/vec

The vec keyword
The function vec treats the string in EXPRESSION as a bit vector with elements of width BITS and returns the value of the element specified by OFFSET as an unsigned integer. BITS must be a power of two from 1 to 32 (or 64, if the platform supports it).

If BITS is 8, the elements of the input string are bytes.

If BITS is 16 or more, bytes of the input string are grouped into chunks of size <tt>BITS</tt>/8. Each group is converted to a number as it is done with <tt>pack</tt>/<tt>unpack</tt> using the big-endian formats <tt>n /N</tt> (analogously for <tt>BITS==64</tt>).

If <tt>BITS</tt> is <tt>4</tt> or less, the string is broken into bytes, then the bits of each byte are broken into 8/<tt>BITS</tt> groups. Bits of a byte are numbered in a little-endian-ish way, as in <tt>0x01</tt>, <tt>0x02</tt>, <tt>0x04</tt>, <tt>0x08</tt>, <tt>0x10</tt> etc

<tt>vec</tt> may also be assigned to, in which case parentheses are needed to give the expression the correct precedence. See example below. If the selected element is outside the string, 0 is returned. If an element off the end of the string is written to, Perl will first extend the string with sufficiently many zero bytes. Negative <tt>OFFSET</tt> is considered an error.

<tt>vec</tt> ignores the UTF-8 flag, if it happens that the string is encoded as UTF-8 internally, and it operates on the internal byte string.