XN--SZS37G.COM
welcome to my space
X
Article search:  
 HOME   Quick Method Using Xor to Determine Wether Number is Power of 2 Using C
Quick Method Using Xor to Determine Wether Number is Power of 2 Using C
Published by: jack 2008-05-24

Level - Intermediate

Well if we have to determine wether a number is a power of 2 there can be tons of algorithm designed to do so using loops and conditional statements. But often there is constraint on program coded to perform under a certain complexity or efficiency.

You really dont want to waste your precious CPU processing for trivial task using giant loop or a switch statement or anything like it. Here i give you a better option, one unmatched in efficiency by any other algorithm which can do this task.

We use a mathematical concept that if we have to find wether X is a power of 2 then X ^ X-1 ( ^ Stands for XOR logic operation ) will return 0 value only when the X is a power of 2.

Now to demonstrate it using coding we will be creating a subroutine in the code below which take them number to be evaluated as input and will return 0 if number is a power of 2 and 0 otherwise.

PLCS.net - Interactive Q & A - The end of ladder logic?::
Seems like it depended on wether you learned old time relay logic and basic What I do have is a "method" of using pre-made forms to outline my I/O.
http://www.plctalk.net/qanda/archive/index.php/t-15722.html
HOME

Also we need to take case of special case in which number to evaluated is 0, which will also produce 0 as return value eventhough 0 is not a multiple of 2.

Source code below is written in C and is implementation of concept, how to determine the number is a power of 2.

#include
int ispowerof2 (int x) // Our Function{
if (!x || (x&(x-1)))
return 1;
else
return 0;
}

int main ()
{
int number;
printf (nEnter A Number : );
scanf (%d, &number);

if (ispowerof2 (number))
{
printf (nnNot a power of 2);
}
else
{
printf (nnPower of 2);
}

return 0;

}

For More Articles like this see my personal blog www.cencyclopedia.com




Pre-Article:10 Reasons - Why C Should be your First Programming Language
Next-Article:Discover the Romance of a Smoky Mountain Wedding

Recent articles

  • Unhealthy Bones
  • How to Choose Great Looking Wedding Shoes For A Beach Wedding
  • Motoring Misery in the UK
  • Online Games Becoming Increasingly Addictive
  • Yellow Pages India Can be your Guide in the Country
  • Vegetable Waste Oil Fuel Helps College Grad Land Dream Job!
  • Get Some Fresh Air, to Help Hair Grow Faster
  • Christmas and the Ghost of Forex Past
  • Periorbital Cellulitis Facts and Treatment
  • Getting your New Business Off the Ground
  • The Mothodology(mmpb)
  • Sports Utility Vehicles are the Coolest to Drive
  • Top Reasons For Link Building
  • Above Ground Pools - Better Than Ever
  • Getting a Job Difficult With Arthritis
  • Eliminating Home Theater Hum and Noise
  • Phoenix Area Tourist Destinations
  • Incentives Are Also Rebates
  • 5 Secrets To Short Sale Success In Foreclosure Investing
  • Good Tastes With Food Magazine
  • How to Master the Art of Small Talk
  • How hard is an online criminal justice class?
  • Nokia 6300: Mind Blowing Charm
  • Cheap Flight to Mumbai Can be Quite Easy to Find
  • What Are the Best Gambling Ebooks?
  • Review of Male Enlargement Devices
  • Tennis Elbow - Injury Treatment Information and Advice
  • Costs, Likelihood of Success, and Timing for a Trademark’s International Branding
  • Using Web Site Promotion Services To Market Your Web Site
  • Link Exchanging – Still an Effective Way of Promoting your Website?
  • Reflexology Certification Programs
  • EarthLink and IP Unity Enable VoIP
  • Basics on Copyrighting your Photographs
  • Absolutley the Best Ab Workout in the World
  • The Times They are A’changin’

  • Suggested articles

  • The Aftermath of a Poor Credit History
  • Guangzhou Honda to Establish a Wholly-owned R&d Subsidiary in China
  • Your Credit Report Could Sink Your Job Prospects! 5 Critical Tips!
  • Does the Nationwide Housing Market Tell the Same Story for Charlottesville?
  • Eczema on Face – Why you Should Adapt your Eczema Meals
  • One Baby Bash Coming Up!!!!
  • Selling A Home Privately
  • The Truth About Protein
  • Pro Basketball General Managers And Scouts
  • Exercise and Effective Weight Loss
  • Hypnosis Helps Cure your Jealousy
  • Chronic Bad Breath-Do You Suffer With It?
  • Depression: Why Do you Feel Blue?
  • Home for Headaches
  • Sony Ericsson W910i Black Mobile Phone Overview!
  • Tis the Season to be Jolly, or is It?
  • Introducing Cats to Dogs
  • Discover Proven Simple Techniques That Generate Thousands Monthly
  • Plan an Attractive Myspace Background With Freshness
  • Get Rid of Stubborn Body Fat With Interval Training
  • Bad Credit Payday Loans UK - Guaranteed Cash Advance
  • Home Based Business-Online Internet Web Marketing is a Must!
  • Powerpoint Presentations Are Great, But Are You Engaging Your Audience?
  • Fiji Is The Soft Coral Capital of the World
  • Home Security 101 - The Basics Of Securing Your Home
  • What Is Cellulite And How Can It Be Safely Removed?
  • Dinner Options for Those With Busy Business Schedules
  • Doing Business in Russia..
  • Hidden Joys Of Baseballs Mascots
  • Spyker’s Rookie Spins on Practice
  • How To Save Money On Your Next Trip To Hawaii
  • Cat Health Insurance: Get Exclusive Care for your Cat
  • The Fast Track to your Financial Freedom (part 1) - Leveraging your Money


  • About us -Site map -Advertisement -Jion us -Contact usExchange linksSponsor us
    Copyright© 2008 xn--szs37g.com All Rights Reserved
    Site made&Support support@xn--szs37g.com    E-mail: web@xn--szs37g.com