Forgetting how to use vec

A

Aaron Sherman

Sorry, I posted this to comp.lang.perl by accident. Here's a repost:

I have a small piece of Perl code that does something like this:

for($i=0;$i<length($x);$i++){
vec($y,$i*7,8)=vec($x,$i*8,8);
}

But it doesn't pack 7-bit data from $x into $y the way I thought it
would. Could someone enlighten me on how I'm mis-reading this? What I
really want is to pack the low 7 bits of $x into $y, with no
high-bit-padding.
 
W

Walter Roberson

: for($i=0;$i<length($x);$i++){
: vec($y,$i*7,8)=vec($x,$i*8,8);
: }

:But it doesn't pack 7-bit data from $x into $y the way I thought it
:would. Could someone enlighten me on how I'm mis-reading this? What I
:really want is to pack the low 7 bits of $x into $y, with no
:high-bit-padding.

$i*7 and $i*8 are offsets. Your code does this if $x is 'PerlOneTwo'

sets byte #0 of y to byte #0 of x 'P'
sets byte #7 of y to byte #8 of x 'w'
sets byte #14 of y to 0 because byte #16 is outside x
extends y to 22 bytes and sets byte #21 to 0 because byte #24 is outside x
extends y to 29 bytes and sets byte #28 to 0 because byte #32 is outside x
....
extends y to 71 bytes and sets byte #70 to 0 because byte #80 is outside x


If you want to slice 7 bits from x into y, then you'd better
work with a BITS of 1 instead of 8.

Or you might find it easier to use unpack to convert the two into
binary strings, do the string manipulations, and pack the result back
together afterwards.
 
A

Aaron Sherman

: for($i=0;$i<length($x);$i++){
: vec($y,$i*7,8)=vec($x,$i*8,8);
: }
$i*7 and $i*8 are offsets. Your code does this if $x is 'PerlOneTwo'

That part I knew. What I always forget is that the BITS argument is
not a length like it is for substr, but a block-size, used to compute
the number of bytes (not bits, even though it's measured in bits) of
the OFFSET.

This is annoying because it means that the above does not work the way
the documentation seems at first glance to suggest, and worse:

vec($y,$i,7)=vec($x,$i,8);

which is EXACTLY what I want does not work because vec is really only
operating in bytes, even though that third parameter is in bits, so it
throws an error about 7 bits being invalid :-(

I think it's time for me to write a module that implements a generic
version of vec (obviously with a different calling convention).
 
D

Darren Dunham

Aaron Sherman said:
I think it's time for me to write a module that implements a generic
version of vec (obviously with a different calling convention).

There is already Bit::Vector which may be useful.
 
A

Anno Siegel

Darren Dunham said:
There is already Bit::Vector which may be useful.

That is a huge powerful module, often the right choice when your problem
hinges on massive bit manipulation. For casual use Simon Cozens'
Bit::Vector::Minimal is an alternative.

Anno
 
A

Aaron Sherman

That is a huge powerful module, often the right choice when your problem
hinges on massive bit manipulation. For casual use Simon Cozens'
Bit::Vector::Minimal is an alternative.

Ah, thanks! I had looked at B::V and determined that it was too
heavyweight for what I wanted. I'll look at B:V:M for future efforts.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top