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

Answer Posted / ahmed ihmeid

bool isPowerOf2(int num)
{
float x;

x = num % (sqrt(num));

if( x == 0)
return true; //it is a power of 2
else
return false; //it is not
}

proof:
4 % 2 = 0
16 % 4 = 0
22 % 4.7 != 0

Is This Answer Correct ?    1 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is int main () in c?

630


What is function and its example?

630


What is the heap in c?

647


How can I find out if there are characters available for reading?

649


What are types of functions?

570






How can I split up a string into whitespace-separated fields?

573


What is external variable in c?

617


Explain what are multidimensional arrays?

607


what are # pragma staments?

1632


Explain function?

665


5 Write an Algorithm to find the maximum and minimum items in a set of ā€˜nā€™ element.

1584


What is a sequential access file?

650


What are c preprocessors?

681


When c language was developed?

644


What are structure members?

601