Perl 中的 vec函数

首页 / Perl入门教程 / Perl 中的 vec函数

描述

此函数使用指定的字符串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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

如何做好一场技术演讲 -〔极客时间〕

深入拆解Tomcat & Jetty -〔李号双〕

ZooKeeper实战与源码剖析 -〔么敬国〕

Electron开发实战 -〔邓耀龙〕

Web安全攻防实战 -〔王昊天〕

Redis核心技术与实战 -〔蒋德钧〕

Django快速开发实战 -〔吕召刚〕

编程高手必学的内存知识 -〔海纳〕

徐昊 · TDD项目实战70讲 -〔徐昊〕

好记忆不如烂笔头。留下您的足迹吧 :)