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
What are the different types of linkage exist in c?
explain what is fifo?
Where can I get an ansi-compatible lint?
What does dm mean sexually?
hi friends how r u as soon in satyam my interview is start but i m very confusued ta wat i do plz help me frndz wat can i do plz tell me some question and answers related with "C" which r asked in the interview .
Can we declare function inside main?
Explain what is the benefit of using enum to declare a constant?
An organised method of depicting the use of an area of computer memory used to signify the uses for different parts of the memory a) swap b) extended memory c) memory map d) all of the above
Explain the properties of union. What is the size of a union variable
What are the properties of union in c?
What is an expression?
What is %s and %d in c?
What is call by reference in functions?
What is file in c language?
What is array in c with example?