此函数使用指定的字符串EXPR作为无符号整数的向量。 NUMBITS参数是为位向量中的每个条目保留的位数。
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 ^.
以下是此函数的简单语法-
vec EXPR, OFFSET, BITS
此函数返回由OFFSET指定的位字段的值。
以下是显示其基本用法的示例代码-
#!/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");
执行上述代码后,将产生以下输出-
vec() Has a created a string of nybbles, in hex: 0001000a0003000f
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)