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

Is main is a keyword in c?

611


Write a program to swap two numbers without using the third variable?

598


What is the symbol indicated the c-preprocessor?

697


any restrictions have on the number of 'return' statements that may be present in a function. a) no restriction b) only 2 return statements c) only 1 return statements d) none of the above

662


What is difference between structure and union?

603






How can I remove the trailing spaces from a string?

617


Are pointers integer?

552


What is the difference between %d and %i?

597


Explain data types & how many data types supported by c?

587


how to make a scientific calculater ?

1566


Explain what is the difference between far and near ?

653


Apart from dennis ritchie who the other person who contributed in design of c language.

814


How do you convert a decimal number to its hexa-decimal equivalent.Give a C code to do the same

650


What are the advantages of using macro in c language?

595


What is string length in c?

615