Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: How can I tell if a number is a power of 2?

by archen (Pilgrim)
on Jan 28, 2002 at 23:12 UTC ( [id://142151]=note: print w/replies, xml ) Need Help??


in reply to How can I tell if a number is a power of 2?

err, I have no idea how this would benchmark, but couldn't you do
return ! $number % 2;
You'd pay a penalty in the calculation, but by the time you get done with sprintf, pattern matching, and a conditional I think the gap would be a lot closer.

Originally posted as a Categorized Answer.

Replies are listed 'Best First'.
Re: Re: How can I tell if a number is a power of 2?
by VSarkiss (Monsignor) on Jan 28, 2002 at 23:16 UTC

    Oops! You're testing whether $number is odd or even, not whether it's a power of 2.

Log In?
Username:
Password:

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

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

    No recent polls found