Perl Programming/Keywords/vec

Previous: values Keywords Next: wait

The vec keyword

edit

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 BITS/8. Each group is converted to a number as it is done with pack()/unpack() using the big-endian formats n /N (analogously for BITS==64).

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

vec 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 OFFSET is considered an error.

vec 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.

Syntax

edit
  vec EXPRESSION, OFFSET, BITS

Examples

edit
vec($largeimage, $max_x*$x + $y, 8) = 3;

See also

edit
Previous: values Keywords Next: wait