Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: Implementing a signed/sign-propagating right bitwise shift

by jethro (Monsignor)
on Feb 16, 2012 at 18:01 UTC ( [id://954306]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Implementing a signed/sign-propagating right bitwise shift
in thread Implementing a signed/sign-propagating right bitwise shift

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)

Replies are listed 'Best First'.
Re^4: Implementing a signed/sign-propagating right bitwise shift
by LonelyPilgrim (Beadle) on Feb 16, 2012 at 18:29 UTC
    Thanks; I get it now. The reason I had the loop in the first place is because that was how I had to work the math in my head: one step at a time, like a monkey with a scratch pad. (I also originally had a print statement at every step of the loop to be sure that it was doing what I thought it was doing!)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://954306]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-23 20:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found