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
Explain modulus operator. What are the restrictions of a modulus operator?
Which are low level languages?
How can I generate floating-point random numbers?
What is the difference between near, far and huge pointers?
find the value of y y = 1.5x+3 for x<=2 y = 2x+5 for x>2
Why malloc is faster than calloc?
Explain how do you view the path?
What is the difference between a function and a method in c?
What is the use of the function in c?
Why are all header files not declared in every c program?
How can you determine the maximum value that a numeric variable can hold?
how logic is used
What is bubble sort technique in c?
How can I invoke another program or command and trap its output?
What is the use of typedef in c?