Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: reliable get position of leftmost bit in large integers

by wollmers (Scribe)
on Mar 12, 2015 at 12:38 UTC ( [id://1119776]=note: print w/replies, xml ) Need Help??


in reply to Re: reliable get position of leftmost bit in large integers
in thread reliable get position of leftmost bit in large integers

Works perfect. I only changed the testing-lines for better visability:

for (map 1 << $_, 0 .. 63) { print sprintf("%b", $_- 1 )," : ", sprintf("%b",nextPowerOfTwo( $_- +1 )); } $ perl leftmost_bit.pl 0 : 0 1 : 1 11 : 100 111 : 1000 1111 : 10000 ...

Maybe it's fast enough versus log(),division,log() if inlined. But I am only prototyping the pure Perl version for a C implementation. But it would be nice to have pure Perl as a fallback.

Replies are listed 'Best First'.
Re^3: reliable get position of leftmost bit in large integers
by BrowserUk (Patriarch) on Mar 12, 2015 at 13:13 UTC
    I am only prototyping the pure Perl version for a C implementation.

    If your doing it in C, it is very efficient compared to other methods; and works well as an Inline::C routine.

    It originates from Bit Twiddling Hacks.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

      I knew the Bit Twiddling page. But I hoped to get it shorter.

      Sure, pure C does not have the overhead.

      Helmut Wollmersdorfer

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 04:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found