Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

Function to find the given number is a power of 2 or not?

Answer Posted / hassan noureddine

To be a power of 2 number,is to have a single 1 bit, and the
rest bits are zeros, lik2 1, 2, 4 , 8, 16, 32, 64, 128, ...

the bitsize of the number is sizeof(number) * 8

isPowerOf2() returns 1 if successful, or 0 (false) otherwise
int isPowerOf2 (number)
{
int foundOnes = 0;
int bitsize = sizeof(number) * 8;

for (i = 0; i < bitsize; i++)
{
if (number & 1)
{
if(++foundOnes > 1)
return false;
/* shift the number to the right */
number >> 1;
}
}
return foundOnes;
}

Is This Answer Correct ?    20 Yes 12 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

why return type of main is not necessary in linux

2155


Difference between strcpy() and memcpy() function?

1220


Explain indirection?

1184


What is || operator and how does it function in a program?

1166


What does sizeof int return?

1138


Explain what is the purpose of "extern" keyword in a function declaration?

1129


Tell me what is the purpose of 'register' keyword in c language?

1046


Explain output of printf("Hello World"-'A'+'B'); ?

1565


What are register variables? What are the advantage of using register variables?

1242


Which is better between malloc and calloc?

1257


What is optimization in c?

1073


while initialization of array why we use a[][2] why not a[2][]...?

2404


What are the back slash character constants or escape sequence charactersavailable in c?

1234


What is a null pointer in c?

1317


What is the right type to use for boolean values in c? Is there a standard type?

1068