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 / binil kuriachan

void main()
{
int a;
printf(" enter the values of x");
scanf("%d",&x);

if(a&1) //or if(a&(a-1)) return true if not power of 2
printf(" \n not a power of 2 ");
else("printf("power of 2");

getch();
}

Is This Answer Correct ?    1 Yes 1 No

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

Answer / anvesh t

if ((0 != n) && !(n & n-1))

Is This Answer Correct ?    0 Yes 0 No

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

Answer / natmat

printf("%u=%u\n", x, ((x & ~(x - 1)) == x));

Is This Answer Correct ?    1 Yes 2 No

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

Answer / diwakar

int main()
{
int x=798;
int a=0;
a=(~x)&1;
if(a)
printf("Even");
else
printf("odd");
return 0;
}

Is This Answer Correct ?    0 Yes 1 No

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

Answer / valli

#include<stdio.h>
main()
{
int n,c=0,i;
for(i=0;i<(sizeof(n)*8);i++)
if(n&(1<<i))
c++;
if(c==1)
printf("%d is powwer of 2",n);
else
printf("%d is not a power of 2",n);
}

Is This Answer Correct ?    0 Yes 2 No

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

Answer / xyz

main()
{

int a[30];
int i=0;
for(i=0; i<30; i++)
a[i]=i;
for(i=0; i<30;i++)
{
if(!(a[i] & a[i-1]))
printf("%d is power of 2\n",a[i]);
else
printf("%d is not a power of 2\n",a[i]);
}



}

Is This Answer Correct ?    0 Yes 3 No

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

Answer / lomesh

#include<stdio.h>

main()
{
int a;
printf("Enter the Positive Number > 0");
scanf("%d",&a);
if ((a&1)==0)
{
printf ("Number Is POWER off 2");
}
else
{
printf ("Number Is NOT power of 2");
}
getch();
}

Is This Answer Correct ?    0 Yes 4 No

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

Answer / nikos

x & (x - 1) == x + (x - 1)

Is This Answer Correct ?    0 Yes 7 No

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

Answer / sathish

main()
{
int a;
scanf("%d",&a);
if((a+(a-1))==((a<<1)-1))
printf("it is powers of 2");
else
printf("not powers of 2");
}

Is This Answer Correct ?    15 Yes 25 No

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

Answer / raghavendra donnur

#include<stdio.h>

void main()
{
int a,i;
scanf("%d",&a);

for( i = 0; a != 0; a = a >> 1)
if( a & 0x01 )
i++;
if( i == 1 )
printf ("POWER off 2");
else
printf (" Not power of 2");
}

Is This Answer Correct ?    1 Yes 11 No

Post New Answer

More C Code Interview Questions

Give a very good method to count the number of ones in a 32 bit number. (caution: looping through testing each bit is not a solution)

7 Answers   Microsoft,


main() { int c=- -2; printf("c=%d",c); }

1 Answers   TCS,


void main() { int *i = 0x400; // i points to the address 400 *i = 0; // set the value of memory location pointed by i; }

2 Answers  


What is the output of the program given below main() { signed char i=0; for(;i>=0;i++) ; printf("%d\n",i); }

1 Answers  


main() { show(); } void show() { printf("I'm the greatest"); }

2 Answers  


main() { char *str1="abcd"; char str2[]="abcd"; printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd")); }

1 Answers  


main() { int i, j; scanf("%d %d"+scanf("%d %d", &i, &j)); printf("%d %d", i, j); } a. Runtime error. b. 0, 0 c. Compile error d. the first two values entered by the user

2 Answers   HCL,


what is variable length argument list?

2 Answers  


main() { char s[ ]="man"; int i; for(i=0;s[ i ];i++) printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]); }

1 Answers   DCE,


What are segment and offset addresses?

2 Answers   Infosys,


main() { char *p = “ayqm”; char c; c = ++*p++; printf(“%c”,c); }

1 Answers  


void main() { int k=ret(sizeof(float)); printf("\n here value is %d",++k); } int ret(int ret) { ret += 2.5; return(ret); }

1 Answers  


Categories