Perl vec Function



Description

This function 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 ^.

Syntax

Following is the simple syntax for this function −

vec EXPR, OFFSET, BITS

Return Value

This function returns the value of the bit field specified by OFFSET.

Example

Following is the example code showing its basic usage −

#!/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");

When above code is executed, it produces the following result −

vec() Has a created a string of nybbles, in hex: 0001000a0003000f
perl_function_references.htm
Advertisements