#include //_________________ #include //_________________ int ispowerOfTwo (unsi
ID: 3863297 • Letter: #
Question
#include //_________________ #include //_________________ int ispowerOfTwo (unsigned int n);//_________________ int main() {unsigned int x = 32; if (ispowerOfTwo (x))//_________________ printf ("%d is indeed a power of 21 ", x);//_________________ else printf ("Notequalto luck here, %d is not a power of 2 ", x);//_________________ return 0;} int ispowerOfTwo (unsigned int n)//_________________ {unsigned int bitcount = 0;//_________________ while (n! = 0) {//_________________ if (n & 1) {//_________________ bitcount + +;//_________________} n = n >> 1;//_________________} if (bitcount = 1) return 1; return 0;//_________________}Explanation / Answer
Answer:
#include<stdio.h> //header file for input output functions
#include<stdlib.h> //header file for including pre defined libraries to our program
int ispowerOfTwo(unsigned int n); //function prototype
int main()
{
unsigned int x=32;
if(ispowerOfTwo(x)) //checking the function and is passed under if condition
printf("%d is indeed power of 2! ",x); //it will be printed when the condition under if is true that is the number is power of two.
else
printf("No luck here,%d is not a power of two ",x); //it will be printed when the condition under if is false.
return 0;
}
int ispowerOfTwo(unsigned int n) //now the function is defined that is function definition
{
unsigned int bitcount=0; //declaration of unsigned integer and initializing it to 0;
while(n!=0) //initializing while loop and will terminate when value of n will be equal to 0
if(n&1) //checking the condition and will be executed only when first condition is true
{
bitcount++; //incrementing the unsigned int variable
}
n=n>>1; //right shifting the value of n by 1 place;
if(bitcount==1)
return 1;
return 0; // since the return type of function is int. Therefore, the function will be returning a value.
Hence, return 0 is applied.
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.