Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Power of two round up.

by I0 (Priest)
on Dec 15, 2000 at 23:41 UTC ( [id://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 ++$_

Replies are listed 'Best First'.
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;


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

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-07-21 14:41 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.