XP is just a number | |
PerlMonks |
Re^3: Implementing a signed/sign-propagating right bitwise shiftby jethro (Monsignor) |
on Feb 16, 2012 at 18:01 UTC ( [id://954306]=note: print w/replies, xml ) | Need Help?? |
a) if you do something in a loop that is executed 32 times, that something is done 32 times. If you do it before the loop it is only done once. That should make anything faster. So you could do this:
As you can see the bitshift is only done once and inside the loop the constant contents of the variable is used instead. b) What you do is (for negative 32bit numbers): loop $y times: shift by one, set the high bit, shift by one, set the high bit ... What you could do: No loop, just shift by $y. set all high bits at once (With high bits I mean all the bits shifted in as 0 instead of 1). And the value you need to set all high-bits at once would be what? 0b10000... if $y==1, 0b11000.... if $y==2, and so on to 0b1111... if $y=32. This value is just 0b111111.... shifted left 32-$y times, i.e. 0b1111.... << 32-$y. More generally I would specify that as -1 << ($longsize*8 - $y)
In Section
Seekers of Perl Wisdom
|
|