The vec function, what is it for and where can it be avoided in principle ?

  • Approximately for the same for what in C it is possible to do bit fields in structures (which with a colon). You can do without it everywhere. Bits can also be controlled by bit operations or bytes can be formed using pack / unpack. It's just easier working with her with bats - Mike

1 answer 1

Description:

vec EXPR, OFFSET, BITS 

This function uses the specified EXPR string as an unsigned integer vector. The NUMBITS parameter is the number of bits reserved for each entry in the bit vector.

Note that the offset is a marker for the end of the vector, and it counts the number of bits specified for the start search. Vectors can be controlled by logical bitwise operators |, &, and ^.

 #!/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 print("vec(), in hex: ", unpack("h*", $vec), "\n"); 

Result:

 vec(), in hex: 0001000a0003000f