Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Forgetting how to use vec

Reply
Thread Tools

Forgetting how to use vec

 
 
Aaron Sherman
Guest
Posts: n/a
 
      02-06-2004
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,=vec($x,$i*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.
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      02-06-2004
In article <(E-Mail Removed) >,
Aaron Sherman <(E-Mail Removed)> wrote:
:I have a small piece of Perl code that does something like this:

: for($i=0;$i<length($x);$i++){
: vec($y,$i*7,=vec($x,$i*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.
--
Look out, there are llamas!
 
Reply With Quote
 
 
 
 
Aaron Sherman
Guest
Posts: n/a
 
      02-11-2004
http://www.velocityreviews.com/forums/(E-Mail Removed)-cnrc.gc.ca (Walter Roberson) wrote in message news:<c00r3f$3f4$(E-Mail Removed)>...
> In article <(E-Mail Removed) >,
> Aaron Sherman <(E-Mail Removed)> wrote:
> :I have a small piece of Perl code that does something like this:
>
> : for($i=0;$i<length($x);$i++){
> : vec($y,$i*7,=vec($x,$i*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,;

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).
 
Reply With Quote
 
Darren Dunham
Guest
Posts: n/a
 
      02-12-2004
Aaron Sherman <(E-Mail Removed)> wrote:
> 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.

--
Darren Dunham http://www.velocityreviews.com/forums/(E-Mail Removed)
Senior Technical Consultant TAOS http://www.taos.com/
Got some Dr Pepper? San Francisco, CA bay area
< This line left intentionally blank to confuse you. >
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      02-12-2004
Darren Dunham <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Aaron Sherman <(E-Mail Removed)> wrote:
> > 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.


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
 
Reply With Quote
 
Aaron Sherman
Guest
Posts: n/a
 
      02-13-2004
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Anno Siegel) wrote in message news:<c0gvk4$jh8$(E-Mail Removed)-Berlin.DE>...
> Darren Dunham <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> > Aaron Sherman <(E-Mail Removed)> wrote:
> > > 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.

>
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
(&vec)== &vec[0]? Ioannis Vranos C++ 5 10-01-2008 02:27 PM
is vec.reserve(unsigned int) better than vec(unsigned int)? er C++ 6 09-14-2007 06:20 AM
vec - a misunderstanding Mark Hobley Perl 1 03-18-2006 07:32 PM
Forgetting how to use vec Aaron Sherman Perl 0 02-05-2004 06:50 PM



Advertisments