Copyright © tutorialspoint.com

# PERL vec Function

## Syntax

vec EXPR, OFFSET, BITS

## Definition and Usage

Uses the string specified EXPR as a vector of unsigned integers. The NUMBITS parameter is 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. Note that the offset is the marker for the end of the vector, and it counts back the number of bits specified to find the start. Vectors can be manipulated with the logical bitwise operators |, & and ^.

## Return Value

- The value of the bit field specified by OFFSET.

## Example

    #!/usr/bin/perl -w

    $vec = '';
    vec($vec,  3, 4) = 1;  # bits 0 to 3
    vec($vec,  7, 4) = 10; # bits 4 to 7
    vec($vec, 11, 4) = 3;  # bits 8 to 11
    vec($vec, 15, 4) = 15; # bits 12 to 15
    # As there are 4 bits per number this can
    # be decoded by unpack() as a hex number
    print("vec() Has a created a string of nybbles,
        in hex: ", unpack("h*", $vec), "\n");

It will produce following results:

    vec() Has a created a string of nybbles,
        in hex: 0001000a0003000f

Copyright © tutorialspoint.com