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

Set up procedure for generating a wire frame display of a polyhedron with the hidden edges of the object drawn with dashed lines

0 Answers   IBM,


Write a c program to search an element in an array using recursion

1 Answers   Wipro,


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

1 Answers  


#define a 10 int main() { printf("%d..",a); foo(); printf("%d..",a); return 0; } void foo() { #undef a #define a 50 }

3 Answers  


main() { char not; not=!2; printf("%d",not); }

1 Answers  






# include <stdio.h> int one_d[]={1,2,3}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); }

1 Answers  


how can i search an element in an array

2 Answers   CTS, Microsoft, ViPrak,


main() { int c = 5; printf("%d", main||c); } a. 1 b. 5 c. 0 d. none of the above

2 Answers   HCL,


Finding a number which was log of base 2

1 Answers   NetApp,


main() { int i, j, *p; i = 25; j = 100; p = &i; // Address of i is assigned to pointer p printf("%f", i/(*p) ); // i is divided by pointer p } a. Runtime error. b. 1.00000 c. Compile error d. 0.00000

3 Answers   HCL,


#define prod(a,b) a*b main() { int x=3,y=4; printf("%d",prod(x+2,y-1)); }

1 Answers  


Is this code legal? int *ptr; ptr = (int *) 0x400;

1 Answers  


Categories