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 what is the most efficient way to store flag values?

688


Which is not valid in C a) class aClass{public:int x;}; b) /* A comment */ c) char x=12;

601


What are linked lists in c?

641


What is graph in c?

574


Explain with the aid of an example why arrays of structures don’t provide an efficient representation when it comes to adding and deleting records internal to the array.

2643






What is structure pointer in c?

564


Can you please explain the difference between malloc() and calloc() function?

609


What is the condition that is applied with ?: Operator?

654


What are the string functions? List some string functions available in c.

594


What are the advantages of c preprocessor?

699


Does c have class?

604


Why cant I open a file by its explicit path?

586


C program to find all possible outcomes of a dice?

1841


Is c programming hard?

563


What are the standard predefined macros?

625