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 why C language is procedural?

988


How can I recover the file name given an open stream or file descriptor?

850


How can I list all of the predefined identifiers?

774


In C programming, how do you insert quote characters (‘ and “) into the output screen?

1244


What is variable initialization and why is it important?

855


What are structures and unions? State differencves between them.

864


Which is better pointer or array?

812


How do you convert strings to numbers in C?

942


What is the difference between typedef struct and struct?

853


How many keywords are there in c?

831


What are formal parameters?

865


How does free() know explain how much memory to release?

829


When can a far pointer be used?

784


hi send me sample aptitude papers of cts?

1908


What is a keyword?

954