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 is variable and explain rules to declare variable in c?
Tell me with an example the self-referential structure?
What does *p++ do?
What type of function is main ()?
why to assign a pointer to null sometimes??how can a pointer we declare get assigned with a garbage value by default???
Can we change the value of constant variable in c?
Differentiate between calloc and malloc.
How do you list files in a directory?
What are the ways to a null pointer can use in c programming language?
What is the role of && operator in a program code?
How can you tell whether a program was compiled using c versus c++?
Is c still used?
What is the difference between array and structure in c?
Can you please explain the difference between malloc() and calloc() function?
Write a progarm to find the length of string using switch case?