vec - a misunderstanding

Discussion in 'Perl Misc' started by Mark Hobley, Mar 18, 2006.

  1. Mark Hobley

    Mark Hobley Guest

    I am trying to understand how the vec function is used.

    I have produced the following code, but the behaviour is not as expected.

    $binstring = '@' ; # 01000000 binary ascii code
    print vec($binstring,0,1); # returns 0, the binary value of bits 0 and 1
    vec($binstring,0,1) = 3; # bits 0 and 1
    print $binstring; # returns A, I am expecting C (binary ascii code 01000011)
    vec($binstring,2,1) = 3; # bits 2 and 3
    print $binstring; # returns E, I am expecting O (binary ascii code 01001111)

    Documentation tells me that vec has the syntax:

    vec EXPR, OFFSET, BITS

    where BITS is a power of 2 from 1 to 32.

    I presumed offset of zero would give me the leftmost (most significant) bit.
    It actually gives me the rightmost bit.

    Is this expected behaviour?

    The number of bits is a power of two ranging from 1 to 32. Presumably this
    means that minimum element size (BITS = 1) is 2^1 = 2 bits, and therefore I
    cannot use single bit elements.

    Is it correct that if BITS = 1, my elements should be 2 bits wide?

    I presumed that the offset was in number of elements, so an offset of 1, would
    point to bits two and three. It actually appears to point to bit 1, which is
    part of the first element.

    Presumably then, offset is in number of bits, not number of elements. Is this
    correct?

    When I set the value of my element to three, indicating both bits set to 1, I
    only appear to be getting a single bit set.

    What is going on here?

    --
    Mark Hobley
    393 Quinton Road West
    QUINTON
    Birmingham
    B32 1QE

    Telephone: (0121) 247 1596
    International: 0044 121 247 1596

    Email: markhobley at hotpop dot donottypethisbit com

    http://markhobley.yi.org/
    Mark Hobley, Mar 18, 2006
    #1
    1. Advertising

  2. Mark Hobley

    John Bokma Guest

    (Mark Hobley) wrote:


    > The number of bits is a power of two ranging from 1 to 32. Presumably
    > this means that minimum element size (BITS = 1) is 2^1 = 2 bits, and
    > therefore I cannot use single bit elements.


    Yes you can, but you can't use 3 or 5, the *number* has to be a power of
    two, so 2^0, 2^1, 2^2, 2^3, 2^4.

    > Is it correct that if BITS = 1, my elements should be 2 bits wide?


    No, it's 1, but you can't use 3 bits wide, since 3 is not a power of 2 (if
    I read the documentation correctly).


    --
    John Bokma Freelance software developer
    &
    Experienced Perl programmer: http://castleamber.com/
    John Bokma, Mar 18, 2006
    #2
    1. Advertising

  3. Mark Hobley

    Anno Siegel Guest

    [newsgroups trimmed]

    Mark Hobley <> wrote in comp.lang.perl.misc:
    > I am trying to understand how the vec function is used.
    >
    > I have produced the following code, but the behaviour is not as expected.
    >
    > $binstring = '@' ; # 01000000 binary ascii code
    > print vec($binstring,0,1); # returns 0, the binary value of bits 0 and 1
    > vec($binstring,0,1) = 3; # bits 0 and 1
    > print $binstring; # returns A, I am expecting C (binary ascii code 01000011)
    > vec($binstring,2,1) = 3; # bits 2 and 3
    > print $binstring; # returns E, I am expecting O (binary ascii code 01001111)
    >
    > Documentation tells me that vec has the syntax:
    >
    > vec EXPR, OFFSET, BITS
    >
    > where BITS is a power of 2 from 1 to 32.


    Quite so, a power of two, not the exponent of two, as you appear to believe.

    > I presumed offset of zero would give me the leftmost (most significant) bit.
    > It actually gives me the rightmost bit.
    >
    > Is this expected behaviour?


    Yes. Bit strings are numbered least significant bit first.

    [misunderstandings snipped]

    With vec, it is useful to look at the third argument (BITS) first.
    It determines in chunks of what size the string is divided. As the
    description says, only powers of 2 (1, 2, 4, ... 32) are allowed, 64
    too if the architecture supports it. The BITS gives the number of
    bits in each chunk. The value of OFFSET is counted in these units,
    it is the 0-based number of the chunk to retrieve.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Mar 18, 2006
    #3
    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:
    593
    Aaron Sherman
    Feb 5, 2004
  2. Mark Hobley

    vec - a misunderstanding

    Mark Hobley, Mar 18, 2006, in forum: Perl
    Replies:
    1
    Views:
    2,600
    John Bokma
    Mar 18, 2006
  3. Pino
    Replies:
    3
    Views:
    268
    Jim Langston
    Jun 14, 2007
  4. er
    Replies:
    6
    Views:
    475
    Andre Kostur
    Sep 14, 2007
  5. Ioannis Vranos

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

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

Share This Page