vec

Perl 5 version 10.1 documentation

vec

  • vec EXPR,OFFSET,BITS

    Treats the string in EXPR as a bit vector made up of elements of width BITS, and returns the value of the element specified by OFFSET as an unsigned integer. BITS therefore specifies the number of bits that are reserved for each element in the bit vector. This must be a power of two from 1 to 32 (or 64, if your platform supports that).

    If BITS is 8, "elements" coincide with bytes of the input string.

    If BITS is 16 or more, bytes of the input string are grouped into chunks of size BITS/8, and each group is converted to a number as with pack()/unpack() with big-endian formats n /N (and analogously for BITS==64). See pack for details.

    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 , 0x20 , 0x40 , 0x80 . For example, breaking the single input byte chr(0x36) into two groups gives a list (0x6, 0x3) ; breaking it into 4 groups gives (0x2, 0x1, 0x3, 0x0) .

    vec may also be assigned to, in which case parentheses are needed to give the expression the correct precedence as in

    1. vec($image, $max_x * $x + $y, 8) = 3;

    If the selected element is outside the string, the value 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. It is an error to try to write off the beginning of the string (i.e. negative OFFSET).

    If the string happens to be encoded as UTF-8 internally (and thus has the UTF8 flag set), this is ignored by vec, and it operates on the internal byte string, not the conceptual character string, even if you only have characters with values less than 256.

    Strings created with vec can also be manipulated with the logical operators |, & , ^, and ~ . These operators will assume a bit vector operation is desired when both operands are strings. See "Bitwise String Operators" in perlop.

    The following code will build up an ASCII string saying 'PerlPerlPerl' . The comments show the string after each step. Note that this code works in the same way on big-endian or little-endian machines.

    1. my $foo = '';
    2. vec($foo, 0, 32) = 0x5065726C; # 'Perl'
    3. # $foo eq "Perl" eq "\x50\x65\x72\x6C", 32 bits
    4. print vec($foo, 0, 8); # prints 80 == 0x50 == ord('P')
    5. vec($foo, 2, 16) = 0x5065; # 'PerlPe'
    6. vec($foo, 3, 16) = 0x726C; # 'PerlPerl'
    7. vec($foo, 8, 8) = 0x50; # 'PerlPerlP'
    8. vec($foo, 9, 8) = 0x65; # 'PerlPerlPe'
    9. vec($foo, 20, 4) = 2; # 'PerlPerlPe' . "\x02"
    10. vec($foo, 21, 4) = 7; # 'PerlPerlPer'
    11. # 'r' is "\x72"
    12. vec($foo, 45, 2) = 3; # 'PerlPerlPer' . "\x0c"
    13. vec($foo, 93, 1) = 1; # 'PerlPerlPer' . "\x2c"
    14. vec($foo, 94, 1) = 1; # 'PerlPerlPerl'
    15. # 'l' is "\x6c"

    To transform a bit vector into a string or list of 0's and 1's, use these:

    1. $bits = unpack("b*", $vector);
    2. @bits = split(//, unpack("b*", $vector));

    If you know the exact length in bits, it can be used in place of the * .

    Here is an example to illustrate how the bits actually fall in place:

    1. #!/usr/bin/perl -wl
    2. print <<'EOT';
    3. 0 1 2 3
    4. unpack("V",$_) 01234567890123456789012345678901
    5. ------------------------------------------------------------------
    6. EOT
    7. for $w (0..3) {
    8. $width = 2**$w;
    9. for (="../index.html">Home > Language reference > Functions > vec

vec

  • vec EXPR,OFFSET,BITS

    Treats the string in EXPR as a bit vector made up of elements of width BITS, and returns the value of the element specified by OFFSET as an unsigned integer. BITS therefore specifies the number of bits that are reserved for each element in the bit vector. This must be a power of two from 1 to 32 (or 64, if your platform supports that).

    If BITS is 8, "elements" coincide with bytes of the input string.

    If BITS is 16 or more, bytes of the input string are grouped into chunks of size BITS/8, and each group is converted to a number as with pack()/unpack() with big-endian formats n /N (and analogously for BITS==64). See pack for details.

    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 , 0x20 , 0x40 , 0x80 . For example, breaking the single input byte chr(0x36) into two groups gives a list (0x6, 0x3) ; breaking it into 4 groups gives (0x2, 0x1, 0x3, 0x0) .

    vec may also be assigned to, in which case parentheses are needed to give the expression the correct precedence as in

    1. vec($image, $max_x * $x + $y, 8) = 3;

    If the selected element is outside the string, the value 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. It is an error to try to write off the beginning of the string (i.e. negative OFFSET).

    If the string happens to be encoded as UTF-8 internally (and thus has the UTF8 flag set), this is ignored by vec, and it operates on the internal byte string, not the conceptual character string, even if you only have characters with values less than 256.

    Strings created with vec can also be manipulated with the logical operators |, & , ^, and ~ . These operators will assume a bit vector operation is desired when both operands are strings. See "Bitwise String Operators" in perlop.

    The following code will build up an ASCII string saying 'PerlPerlPerl' . The comments show the string after each step. Note that this code works in the same way on big-endian or little-endian machines.

    1. my $foo = '';
    2. vec($foo, 0, 32) = 0x5065726C; # 'Perl'
    3. # $foo eq "Perl" eq "\x50\x65\x72\x6C", 32 bits
    4. print vec($foo, 0, 8); # prints 80 == 0x50 == ord('P')
    5. vec($foo, 2, 16) = 0x5065; # 'PerlPe'
    6. vec($foo, 3, 16) = 0x726C; # 'PerlPerl'
    7. vec($foo, 8, 8) = 0x50; # 'PerlPerlP'
    8. vec($foo, 9, 8) = 0x65; # 'PerlPerlPe'
    9. vec($foo, 20, 4) = 2; # 'PerlPerlPe' . "\x02"
    10. vec($foo, 21, 4) = 7; # 'PerlPerlPer'
    11. # 'r' is "\x72"
    12. vec($foo, 45, 2) = 3; # 'PerlPerlPer' . "\x0c"
    13. vec($foo, 93, 1) = 1; # 'PerlPerlPer' . "\x2c"
    14. vec($foo, 94, 1) = 1; # 'PerlPerlPerl'
    15. # 'l' is "\x6c"

    To transform a bit vector into a string or list of 0's and 1's, use these:

    1. $bits = unpack("b*", $vector);
    2. @bits = split(//, unpack("b*", $vector));

    If you know the exact length in bits, it can be used in place of the * .

    Here is an example to illustrate how the bits actually fall in place:

    1. #!/usr/bin/perl -wl
    2. print <<'EOT';
    3. 0 1 2 3
    4. unpack("V",$_) 01234567890123456789012345678901
    5. ------------------------------------------------------------------
    6. EOT
    7. for $w (0..3) {
    8. $width = 2**$w;