Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C Question: Most Economical Test For Integer Is a Power of 2

Reply
Thread Tools

C Question: Most Economical Test For Integer Is a Power of 2

 
 
Old Wolf
Guest
Posts: n/a
 
      01-05-2007
Clark S. Cox III wrote:
> Daniel Pitts wrote:
> > David T. Ashley wrote:
> >> "David T. Ashley" <(E-Mail Removed)> wrote:
> >>> What is the most economical test in 'C' for "integer is a power of 2"?

> >
> > int is_power_of_two(unsigned int value) {
> > return x && (x & (x-1))
> > }

>
> So, your function tells me that 5 and 15 are both powers of 2.


Should be:
x && ! (x & (x-1))

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Economical / administrative problem. Knut Arvid Keilen Computer Support 5 07-31-2005 07:31 AM
Economical / administrative problem. Knut Arvid Keilen Computer Support 6 07-31-2005 07:23 AM
Economical Dial-out Service for US? Mike Schumann VOIP 2 02-06-2005 10:50 PM
Economical way of printing on A4 photo paper Brian Digital Photography 3 08-11-2003 05:45 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments