vec EXPR,OFFSET,BITS

将 EXPR 中的字符串视为由宽度为 BITS 的元素组成的位向量,并返回由 OFFSET 指定的元素的值,该值是一个无符号整数。因此,BITS 指定了位向量中每个元素保留的位数。它必须是 1 到 32(或 64,如果您的平台支持)的 2 的幂。

如果 BITS 为 8,则“元素”与输入字符串的字节一致。

如果 BITS 为 16 或更大,则输入字符串的字节将被分组为大小为 BITS/8 的块,并且每个块将使用 pack/unpack 与大端格式 n/N(以及 BITS==64 的类似情况)进行转换。有关详细信息,请参见 pack

如果位数小于或等于 4,则字符串将被分成字节,然后每个字节的位将被分成 8/BITS 组。字节的位以小端方式编号,例如 0x010x020x040x080x100x200x400x80。例如,将单个输入字节 chr(0x36) 分成两组将得到列表 (0x6, 0x3);将其分成 4 组将得到 (0x2, 0x1, 0x3, 0x0)

vec 也可以被赋值,在这种情况下需要使用括号来给出表达式正确的优先级,例如

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

如果选定的元素在字符串之外,则返回 0。如果写入字符串末尾之外的元素,Perl 将首先用足够多的零字节扩展字符串。尝试写入字符串开头(即负偏移量)是错误的。

如果字符串恰好以 UTF-8 编码在内部(因此设置了 UTF8 标志),vec 会尝试将其转换为使用每个字符一个字节的内部表示。但是,如果字符串包含值为 256 或更高的字符,则会发生致命错误。

使用 vec 创建的字符串也可以使用逻辑运算符 |&^~ 进行操作。当两个操作数都是字符串时,这些运算符将假定需要进行位向量运算。请参阅 "perlop 中的位字符串运算符"

以下代码将构建一个 ASCII 字符串,内容为 'PerlPerlPerl'。注释显示了每个步骤后的字符串。请注意,此代码在大小端机器上以相同的方式工作。

my $foo = '';
vec($foo,  0, 32) = 0x5065726C; # 'Perl'

# $foo eq "Perl" eq "\x50\x65\x72\x6C", 32 bits
print vec($foo, 0, 8);  # prints 80 == 0x50 == ord('P')

vec($foo,  2, 16) = 0x5065; # 'PerlPe'
vec($foo,  3, 16) = 0x726C; # 'PerlPerl'
vec($foo,  8,  8) = 0x50;   # 'PerlPerlP'
vec($foo,  9,  8) = 0x65;   # 'PerlPerlPe'
vec($foo, 20,  4) = 2;      # 'PerlPerlPe'   . "\x02"
vec($foo, 21,  4) = 7;      # 'PerlPerlPer'
                               # 'r' is "\x72"
vec($foo, 45,  2) = 3;      # 'PerlPerlPer'  . "\x0c"
vec($foo, 93,  1) = 1;      # 'PerlPerlPer'  . "\x2c"
vec($foo, 94,  1) = 1;      # 'PerlPerlPerl'
                               # 'l' is "\x6c"

要将位向量转换为字符串或 0 和 1 的列表,请使用以下方法

my $bits = unpack("b*", $vector);
my @bits = split(//, unpack("b*", $vector));

如果您知道确切的位长度,则可以使用它来代替 *

以下是一个示例,说明位是如何实际落位的

#!/usr/bin/perl -wl

print <<'EOT';
                                  0         1         2         3
                   unpack("V",$_) 01234567890123456789012345678901
------------------------------------------------------------------
EOT

for $w (0..3) {
    $width = 2**$w;
    for ($shift=0; $shift < $width; ++$shift) {
        for ($off=0; $off < 32/$width; ++$off) {
            $str = pack("B*", "0"x32);
            $bits = (1<<$shift);
            vec($str, $off, $width) = $bits;
            $res = unpack("b*",$str);
            $val = unpack("V", $str);
            write;
        }
    }
}

format STDOUT =
vec($_,@#,@#) = @<< == @######### @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
$off, $width, $bits, $val, $res
.
__END__

无论运行它的机器架构如何,上面的示例都应该打印以下表格

                                  0         1         2         3
                   unpack("V",$_) 01234567890123456789012345678901
------------------------------------------------------------------
vec($_, 0, 1) = 1   ==          1 10000000000000000000000000000000
vec($_, 1, 1) = 1   ==          2 01000000000000000000000000000000
vec($_, 2, 1) = 1   ==          4 00100000000000000000000000000000
vec($_, 3, 1) = 1   ==          8 00010000000000000000000000000000
vec($_, 4, 1) = 1   ==         16 00001000000000000000000000000000
vec($_, 5, 1) = 1   ==         32 00000100000000000000000000000000
vec($_, 6, 1) = 1   ==         64 00000010000000000000000000000000
vec($_, 7, 1) = 1   ==        128 00000001000000000000000000000000
vec($_, 8, 1) = 1   ==        256 00000000100000000000000000000000
vec($_, 9, 1) = 1   ==        512 00000000010000000000000000000000
vec($_,10, 1) = 1   ==       1024 00000000001000000000000000000000
vec($_,11, 1) = 1   ==       2048 00000000000100000000000000000000
vec($_,12, 1) = 1   ==       4096 00000000000010000000000000000000
vec($_,13, 1) = 1   ==       8192 00000000000001000000000000000000
vec($_,14, 1) = 1   ==      16384 00000000000000100000000000000000
vec($_,15, 1) = 1   ==      32768 00000000000000010000000000000000
vec($_,16, 1) = 1   ==      65536 00000000000000001000000000000000
vec($_,17, 1) = 1   ==     131072 00000000000000000100000000000000
vec($_,18, 1) = 1   ==     262144 00000000000000000010000000000000
vec($_,19, 1) = 1   ==     524288 00000000000000000001000000000000
vec($_,20, 1) = 1   ==    1048576 00000000000000000000100000000000
vec($_,21, 1) = 1   ==    2097152 00000000000000000000010000000000
vec($_,22, 1) = 1   ==    4194304 00000000000000000000001000000000
vec($_,23, 1) = 1   ==    8388608 00000000000000000000000100000000
vec($_,24, 1) = 1   ==   16777216 00000000000000000000000010000000
vec($_,25, 1) = 1   ==   33554432 00000000000000000000000001000000
vec($_,26, 1) = 1   ==   67108864 00000000000000000000000000100000
vec($_,27, 1) = 1   ==  134217728 00000000000000000000000000010000
vec($_,28, 1) = 1   ==  268435456 00000000000000000000000000001000
vec($_,29, 1) = 1   ==  536870912 00000000000000000000000000000100
vec($_,30, 1) = 1   == 1073741824 00000000000000000000000000000010
vec($_,31, 1) = 1   == 2147483648 00000000000000000000000000000001
vec($_, 0, 2) = 1   ==          1 10000000000000000000000000000000
vec($_, 1, 2) = 1   ==          4 00100000000000000000000000000000
vec($_, 2, 2) = 1   ==         16 00001000000000000000000000000000
vec($_, 3, 2) = 1   ==         64 00000010000000000000000000000000
vec($_, 4, 2) = 1   ==        256 00000000100000000000000000000000
vec($_, 5, 2) = 1   ==       1024 00000000001000000000000000000000
vec($_, 6, 2) = 1   ==       4096 00000000000010000000000000000000
vec($_, 7, 2) = 1   ==      16384 00000000000000100000000000000000
vec($_, 8, 2) = 1   ==      65536 00000000000000001000000000000000
vec($_, 9, 2) = 1   ==     262144 00000000000000000010000000000000
vec($_,10, 2) = 1   ==    1048576 00000000000000000000100000000000
vec($_,11, 2) = 1   ==    4194304 00000000000000000000001000000000
vec($_,12, 2) = 1   ==   16777216 00000000000000000000000010000000
vec($_,13, 2) = 1   ==   67108864 00000000000000000000000000100000
vec($_,14, 2) = 1   ==  268435456 00000000000000000000000000001000
vec($_,15, 2) = 1   == 1073741824 00000000000000000000000000000010
vec($_, 0, 2) = 2   ==          2 01000000000000000000000000000000
vec($_, 1, 2) = 2   ==          8 00010000000000000000000000000000
vec($_, 2, 2) = 2   ==         32 00000100000000000000000000000000
vec($_, 3, 2) = 2   ==        128 00000001000000000000000000000000
vec($_, 4, 2) = 2   ==        512 00000000010000000000000000000000
vec($_, 5, 2) = 2   ==       2048 00000000000100000000000000000000
vec($_, 6, 2) = 2   ==       8192 00000000000001000000000000000000
vec($_, 7, 2) = 2   ==      32768 00000000000000010000000000000000
vec($_, 8, 2) = 2   ==     131072 00000000000000000100000000000000
vec($_, 9, 2) = 2   ==     524288 00000000000000000001000000000000
vec($_,10, 2) = 2   ==    2097152 00000000000000000000010000000000
vec($_,11, 2) = 2   ==    8388608 00000000000000000000000100000000
vec($_,12, 2) = 2   ==   33554432 00000000000000000000000001000000
vec($_,13, 2) = 2   ==  134217728 00000000000000000000000000010000
vec($_,14, 2) = 2   ==  536870912 00000000000000000000000000000100
vec($_,15, 2) = 2   == 2147483648 00000000000000000000000000000001
vec($_, 0, 4) = 1   ==          1 10000000000000000000000000000000
vec($_, 1, 4) = 1   ==         16 00001000000000000000000000000000
vec($_, 2, 4) = 1   ==        256 00000000100000000000000000000000
vec($_, 3, 4) = 1   ==       4096 00000000000010000000000000000000
vec($_, 4, 4) = 1   ==      65536 00000000000000001000000000000000
vec($_, 5, 4) = 1   ==    1048576 00000000000000000000100000000000
vec($_, 6, 4) = 1   ==   16777216 00000000000000000000000010000000
vec($_, 7, 4) = 1   ==  268435456 00000000000000000000000000001000
vec($_, 0, 4) = 2   ==          2 01000000000000000000000000000000
vec($_, 1, 4) = 2   ==         32 00000100000000000000000000000000
vec($_, 2, 4) = 2   ==        512 00000000010000000000000000000000
vec($_, 3, 4) = 2   ==       8192 00000000000001000000000000000000
vec($_, 4, 4) = 2   ==     131072 00000000000000000100000000000000
vec($_, 5, 4) = 2   ==    2097152 00000000000000000000010000000000
vec($_, 6, 4) = 2   ==   33554432 00000000000000000000000001000000
vec($_, 7, 4) = 2   ==  536870912 00000000000000000000000000000100
vec($_, 0, 4) = 4   ==          4 00100000000000000000000000000000
vec($_, 1, 4) = 4   ==         64 00000010000000000000000000000000
vec($_, 2, 4) = 4   ==       1024 00000000001000000000000000000000
vec($_, 3, 4) = 4   ==      16384 00000000000000100000000000000000
vec($_, 4, 4) = 4   ==     262144 00000000000000000010000000000000
vec($_, 5, 4) = 4   ==    4194304 00000000000000000000001000000000
vec($_, 6, 4) = 4   ==   67108864 00000000000000000000000000100000
vec($_, 7, 4) = 4   == 1073741824 00000000000000000000000000000010
vec($_, 0, 4) = 8   ==          8 00010000000000000000000000000000
vec($_, 1, 4) = 8   ==        128 00000001000000000000000000000000
vec($_, 2, 4) = 8   ==       2048 00000000000100000000000000000000
vec($_, 3, 4) = 8   ==      32768 00000000000000010000000000000000
vec($_, 4, 4) = 8   ==     524288 00000000000000000001000000000000
vec($_, 5, 4) = 8   ==    8388608 00000000000000000000000100000000
vec($_, 6, 4) = 8   ==  134217728 00000000000000000000000000010000
vec($_, 7, 4) = 8   == 2147483648 00000000000000000000000000000001
vec($_, 0, 8) = 1   ==          1 10000000000000000000000000000000
vec($_, 1, 8) = 1   ==        256 00000000100000000000000000000000
vec($_, 2, 8) = 1   ==      65536 00000000000000001000000000000000
vec($_, 3, 8) = 1   ==   16777216 00000000000000000000000010000000
vec($_, 0, 8) = 2   ==          2 01000000000000000000000000000000
vec($_, 1, 8) = 2   ==        512 00000000010000000000000000000000
vec($_, 2, 8) = 2   ==     131072 00000000000000000100000000000000
vec($_, 3, 8) = 2   ==   33554432 00000000000000000000000001000000
vec($_, 0, 8) = 4   ==          4 00100000000000000000000000000000
vec($_, 1, 8) = 4   ==       1024 00000000001000000000000000000000
vec($_, 2, 8) = 4   ==     262144 00000000000000000010000000000000
vec($_, 3, 8) = 4   ==   67108864 00000000000000000000000000100000
vec($_, 0, 8) = 8   ==          8 00010000000000000000000000000000
vec($_, 1, 8) = 8   ==       2048 00000000000100000000000000000000
vec($_, 2, 8) = 8   ==     524288 00000000000000000001000000000000
vec($_, 3, 8) = 8   ==  134217728 00000000000000000000000000010000
vec($_, 0, 8) = 16  ==         16 00001000000000000000000000000000
vec($_, 1, 8) = 16  ==       4096 00000000000010000000000000000000
vec($_, 2, 8) = 16  ==    1048576 00000000000000000000100000000000
vec($_, 3, 8) = 16  ==  268435456 00000000000000000000000000001000
vec($_, 0, 8) = 32  ==         32 00000100000000000000000000000000
vec($_, 1, 8) = 32  ==       8192 00000000000001000000000000000000
vec($_, 2, 8) = 32  ==    2097152 00000000000000000000010000000000
vec($_, 3, 8) = 32  ==  536870912 00000000000000000000000000000100
vec($_, 0, 8) = 64  ==         64 00000010000000000000000000000000
vec($_, 1, 8) = 64  ==      16384 00000000000000100000000000000000
vec($_, 2, 8) = 64  ==    4194304 00000000000000000000001000000000
vec($_, 3, 8) = 64  == 1073741824 00000000000000000000000000000010
vec($_, 0, 8) = 128 ==        128 00000001000000000000000000000000
vec($_, 1, 8) = 128 ==      32768 00000000000000010000000000000000
vec($_, 2, 8) = 128 ==    8388608 00000000000000000000000100000000
vec($_, 3, 8) = 128 == 2147483648 00000000000000000000000000000001