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

What is variable and explain rules to declare variable in c?

1105


Tell me with an example the self-referential structure?

1042


What does *p++ do?

1029


What type of function is main ()?

1050


why to assign a pointer to null sometimes??how can a pointer we declare get assigned with a garbage value by default???

1985


Can we change the value of constant variable in c?

1067


Differentiate between calloc and malloc.

1284


How do you list files in a directory?

1156


What are the ways to a null pointer can use in c programming language?

1147


What is the role of && operator in a program code?

1084


How can you tell whether a program was compiled using c versus c++?

1119


Is c still used?

1058


What is the difference between array and structure in c?

1182


Can you please explain the difference between malloc() and calloc() function?

1131


Write a progarm to find the length of string using switch case?

2076