Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


Give a oneline C expression to test whether a number is a
power of 2?

Answers were Sorted based on User's Feedback



Give a oneline C expression to test whether a number is a power of 2? ..

Answer / nmk

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

bool isPowerOf2(int number){
if ( number > 0 )
return (number & (number-1)) == 0;

if (number == 0)
return false;

if (isPowerOf2(-number)){
int count=0;
while ( !(number & 1)) {
++count;
number >>= 1;
}
if (fmod(count,2))
return true;
else
return false;
}
return false;
}

int main(void) {
for(int i=-1027; i<1025; ++i){
if (isPowerOf2(i) )
printf("%d\n", i);
}

return EXIT_SUCCESS;
}

Is This Answer Correct ?    2 Yes 14 No

Give a oneline C expression to test whether a number is a power of 2? ..

Answer / ataraxic

n%2

Is This Answer Correct ?    1 Yes 25 No

Give a oneline C expression to test whether a number is a power of 2? ..

Answer / carl menezes

#define ISPOW2(x) ( (x + (x-1) ) == (x<<1 + 1) )

Is This Answer Correct ?    10 Yes 42 No

Give a oneline C expression to test whether a number is a power of 2? ..

Answer / sandhya

#include<stdio.h>
void main()
{
int number;
printf("|n enter a number");
scanf("%d",&number);
int twopower(int);
printf("\n the given number is ");
}
int twopower(int num)
{
while((num & -num)==num)
return(1);
printf("|n yes power of two");
}

Is This Answer Correct ?    9 Yes 45 No

Give a oneline C expression to test whether a number is a power of 2? ..

Answer / mridul

#include<stdio.h>

main()
{
int a;
scanf("%d",&a);
if (a&1)
printf ("POWER off 2");
printf ("NOT power of 2");
}

Is This Answer Correct ?    3 Yes 45 No

Post New Answer

More C Code Interview Questions

main() { int i=0; while(+(+i--)!=0) i-=i++; printf("%d",i); }

9 Answers   CSC, GoDB Tech, IBM,


main() { char *a = "Hello "; char *b = "World"; clrscr(); printf("%s", strcpy(a,b)); } a. “Hello” b. “Hello World” c. “HelloWorld” d. None of the above

4 Answers   Corporate Society, HCL,


Finding a number which was log of base 2

1 Answers   NetApp,


main() { int a=2,*f1,*f2; f1=f2=&a; *f2+=*f2+=a+=2.5; printf("\n%d %d %d",a,*f1,*f2); }

6 Answers  


main() { void swap(); int x=10,y=8; swap(&x,&y); printf("x=%d y=%d",x,y); } void swap(int *a, int *b) { *a ^= *b, *b ^= *a, *a ^= *b; }

2 Answers  


plz tell me the solution.......... in c language program guess any one number from 1 to 50 and tell that number within 8 asking question in yes or no...............

2 Answers   Wipro,


what is brs test reply me email me kashifabbas514@gmail.com

0 Answers  


Write a program to check whether the number is prime and also check if it there i n fibonacci series, then return true otherwise return false

1 Answers   Cognizant, lenovo,


source code for delete data in array for c

1 Answers   TCS,


main() { char a[4]="HELLO"; printf("%s",a); }

3 Answers   CSC,


&#8206;#define good bad main() { int good=1; int bad=0; printf ("good is:%d",good); }

2 Answers  


Sorting entire link list using selection sort and insertion sort and calculating their time complexity

1 Answers   Infosys, Microsoft, NetApp,


Categories