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:

my $highbitset= (1<< $firstbit); foreach ... ... $x & $highbitset) != 0);

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 reply to Re^3: Implementing a signed/sign-propagating right bitwise shift by jethro
in thread Implementing a signed/sign-propagating right bitwise shift by LonelyPilgrim

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.