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

why array index always strats wuth zero?

2 Answers  


Write a program that produces these three columns sequence nos. using loop statement Sequence nos. Squared Squared + 5 1 1 6 2 4 9 3 9 14 4 16 21 5 25 30

1 Answers   GoDB,


int swap(int *a,int *b) { *a=*a+*b;*b=*a-*b;*a=*a-*b; } main() { int x=10,y=20; swap(&x,&y); printf("x= %d y = %d\n",x,y); }

1 Answers  


How do you sort a Linked List (singly connected) in O(n) please mail to pawan.10k@gmail.com if u can find an anser...i m desperate to knw...

6 Answers   Microsoft, MSD, Oracle,


How can i find first 5 natural Numbers without using any loop in c language????????

2 Answers   Microsoft,






How can u say that a given point is in a triangle? 1. with the co-ordinates of the 3 vertices specified. 2. with only the co-ordinates of the top vertex given.

1 Answers  


main() { int i; i = abc(); printf("%d",i); } abc() { _AX = 1000; }

2 Answers  


#define assert(cond) if(!(cond)) \ (fprintf(stderr, "assertion failed: %s, file %s, line %d \n",#cond,\ __FILE__,__LINE__), abort()) void main() { int i = 10; if(i==0) assert(i < 100); else printf("This statement becomes else for if in assert macro"); }

1 Answers  


Write a program to receive an integer and find it's octal equivalent. How can i do with using while loop.

2 Answers  


What is the output for the program given below typedef enum errorType{warning, error, exception,}error; main() { error g1; g1=1; printf("%d",g1); }

1 Answers  


Write a routine to draw a circle (x ** 2 + y ** 2 = r ** 2) without making use of any floating point computations at all.

2 Answers   Mentor Graphics, Microsoft,


writte a c-programm to display smill paces

2 Answers  


Categories