Given an unsigned integer, find if the number is power of 2?

Answer Posted / coder

#include<stdio.h>
void powerOfTwo(int number)
{
if(!(number & number-1) && number)
printf("\nthe number is a power of 2\n");
else printf("\nThe number is not a power of 2\n");
}


int main()
{
powerOfTwo(32); //power of 2
powerOfTwo(22); //not a power of 2
return 0;
}

Is This Answer Correct ?    2 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain modulus operator. What are the restrictions of a modulus operator?

840


Which are low level languages?

877


How can I generate floating-point random numbers?

834


What is the difference between near, far and huge pointers?

849


find the value of y y = 1.5x+3 for x<=2 y = 2x+5 for x>2

1779


Why malloc is faster than calloc?

843


Explain how do you view the path?

908


What is the difference between a function and a method in c?

844


What is the use of the function in c?

835


Why are all header files not declared in every c program?

861


How can you determine the maximum value that a numeric variable can hold?

949


how logic is used

1742


What is bubble sort technique in c?

796


How can I invoke another program or command and trap its output?

891


What is the use of typedef in c?

819