llancet has asked for the wisdom of the Perl Monks concerning the following question:

It just don't work as I thought.
use strict; # generate a 64-bit all-1 mask my $mask; for (my $i=0; $i<64; $i++) { vec($mask,$i,1)=1; } # get a byte of value from 17 my $value = vec($mask,16,8); print "$value\n";
In my opinion, $value should be string "255". Actually,
vec($mask,0,8)
give me that, but with start index other than 0, it always give me "0". So how does it work?

Replies are listed 'Best First'.
Re: how does vec() return?
by almut (Canon) on Sep 25, 2009 at 11:51 UTC

    16 x 8 = 128  (i.e. > 64)

    With

    ... for (0..16) { my $value = vec($mask,$_,8); print "$value\n"; }

    I do get (as expected):

    255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0

    In other words, the offset is in units as specified in width (8 bits here).

      Thanks a lot. I thought that the index was always in bits...