Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Power of two round up.

by I0 (Priest)
on Dec 15, 2000 at 23:41 UTC ( #46899=note: print w/ replies, xml ) Need Help??


in reply to Power of two round up.

local $_ = (shift)-1; $_ |= $_>>1; $_ |= $_>>2; $_ |= $_>>4; $_ |= $_>>8; $_ |= $_>>16; return ++$_


Comment on Re: Power of two round up.
Download Code
Re: Power of two round up.
by Dominus (Parson) on Dec 16, 2000 at 22:24 UTC
    I'm amazed at how clever this is, and I'm going to use it in my program. Thanks a lot.

      Although it's a technique more appropriate in a C program. In Perl, log($_)/log(2) benchmarks faster. (assuming it rounds properly at the boundaries)
      Update: My earlier timethese qq{} may have been misleading. With timethese sub{} the | >> method comes out faster
        Sure, because it has to dispatch many fewer opcodes. But if I had wanted to use the log2 x solution, I wouldn't have asked in the first place. Floating-point arithmetic gives me the heebie-jeebies.

Re: Re: Power of two round up.
by runrig (Abbot) on Dec 18, 2000 at 21:28 UTC
    Just to give Dominus more heebie jeebies :)
    sub round_up { local $_ = (shift)-1; my $num = (2**int(log($_)/log(2)))-1; return ++($_ |= $num); }
Re: Re: Power of two round up.
by jynx (Priest) on Dec 20, 2000 at 06:12 UTC
    On the other hand,

    If you want to use horrible and/or (somewhat) obfuscated code, you could try:

    sub shifty { my $v = (shift)-1; map { return ++$v } map { $v |= $v >> $_ } (1,2,4,8,16); }
    or alternately (the readable version):
    sub shifty2 { my $v = (shift)-1; $v |= $v >> $_ foreach (1,2,4,8,16); return ++$v; }
    i don't know why i was compelled to post that, but i knew one could reduce the repition and possibly obfuscate the code a little with some obviously useless uses of map doing things it wasn't meant to do...
    my $0.02;

    jynx

      #or perhaps $w=32; $v |= $v>>($w>>=1) while $w;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (11)
As of 2014-09-17 18:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (95 votes), past polls