develooper Front page | perl.perl5.porters | Postings from September 2005

Re: [perl #969] shifting of bitvecs considered broken

From:
Jarkko Hietaniemi
Date:
September 27, 2005 23:01
Subject:
Re: [perl #969] shifting of bitvecs considered broken
Message ID:
433A31A8.7040800@iki.fi
Steve Peters via RT wrote:
>>[stmpeters - Wed Jul 20 05:56:11 2005]:
>>
>>
>>>Resubmitting a bug report of mine back from February;
>>>this time through the proper channel (perlbug, not p5p).
>>>
>>>[paste]
>>>
>>>While editing my Damn Book I re-remembered that couple of months back
>>>I ran into an anomaly in the handling of bitvectors.
>>>
>>>Fiction: you have a bitvector which you want to shift.
>>>
>>>The current (5.005_03-MT5) fact:
>>>
>>>perl -wle '$b = ""; vec($b, 0, 1) = 1;print unpack("b*",
>>>$b);$b<<=1;print unpack("b*", $b)'
>>>10000000
>>>00001100
>>>
>>>Huh?  Adding -w tells more, as usual:
>>>Argument "^A" isn't numeric in left_shift at -e line 1.
>>>
>>>So left_shift assumes that the argument to shift is a number, but "^A"
>>>isn't, so it gets converted to string zero "0" (48, 0x30, 0b0000110).
>>>
>>>I consider this behaviour to be rather broken.  I think
>>>
>>>        $b <<= 1
>>>
>>>should shift the whole bitvector left by one position and
>>>
>>>        vec($b, 2, 8) >>= 2
>>>
>>>should shift the bits 16..23 right by two positions (of course not
>>>leaking into bits 8..15).
>>>
>>
>>Just so we're clear, and to add a proper TODO test case, what would you
>>consider the proper output to be?
>>
> 
> 
> This thread sort of went off on a tangent.  What should the expected
> results be?

Well, ASSUMING that there will be in future a way to make shifting of
bitvecs to work as, well, shifting of bitvecs, instead of the shift ops
assuming their arguments are numbers (which must be kept as the default
way of doing things because of hysterical raisins)... assuming this new
pragma is called "bitvec":

perl -Mbitvec -wle '$b = "";
vec($b, 0, 1) = 1;
print unpack("b*",$b);
$b<<=1;
print unpack("b*", $b)'
10000000
01000000

-- 
Jarkko Hietaniemi <jhi@iki.fi> http://www.iki.fi/jhi/ "There is this special
biologist word we use for 'stable'.  It is 'dead'." -- Jack Cohen



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About