Forgetting how to use vec

Discussion in 'Perl Misc' started by Aaron Sherman, Feb 6, 2004.

  1. 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.
    Aaron Sherman, Feb 6, 2004
    #1
    1. Advertising

  2. In article <>,
    Aaron Sherman <> 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,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.
    --
    Look out, there are llamas!
    Walter Roberson, Feb 6, 2004
    #2
    1. Advertising

  3. -cnrc.gc.ca (Walter Roberson) wrote in message news:<c00r3f$3f4$>...
    > In article <>,
    > Aaron Sherman <> 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,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).
    Aaron Sherman, Feb 11, 2004
    #3
  4. Aaron Sherman <> 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
    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. >
    Darren Dunham, Feb 12, 2004
    #4
  5. Aaron Sherman

    Anno Siegel Guest

    Darren Dunham <> wrote in comp.lang.perl.misc:
    > Aaron Sherman <> 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
    Anno Siegel, Feb 12, 2004
    #5
  6. -berlin.de (Anno Siegel) wrote in message news:<c0gvk4$jh8$-Berlin.DE>...
    > Darren Dunham <> wrote in comp.lang.perl.misc:
    > > Aaron Sherman <> 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.
    Aaron Sherman, Feb 13, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aaron Sherman

    Forgetting how to use vec

    Aaron Sherman, Feb 5, 2004, in forum: Perl
    Replies:
    0
    Views:
    592
    Aaron Sherman
    Feb 5, 2004
  2. Mark Hobley

    vec - a misunderstanding

    Mark Hobley, Mar 18, 2006, in forum: Perl
    Replies:
    1
    Views:
    2,598
    John Bokma
    Mar 18, 2006
  3. Snoeys Andy
    Replies:
    2
    Views:
    405
    White Wolf
    Sep 12, 2003
  4. er
    Replies:
    6
    Views:
    473
    Andre Kostur
    Sep 14, 2007
  5. Ioannis Vranos

    (&vec)== &vec[0]?

    Ioannis Vranos, Sep 30, 2008, in forum: C++
    Replies:
    5
    Views:
    417
    Juha Nieminen
    Oct 1, 2008
Loading...

Share This Page