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

union u { struct st { int i : 4; int j : 4; int k : 4; int l; }st; int i; }u; main() { u.i = 100; printf("%d, %d, %d",u.i, u.st.i, u.st.l); } a. 4, 4, 0 b. 0, 0, 0 c. 100, 4, 0 d. 40, 4, 0

1 Answers   HCL,


why is printf("%d %d %d",i++,--i,i--);

4 Answers   Apple, Cynity, TCS,


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

2 Answers  


main() { int i = 257; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }

1 Answers   CSC,


#include <stdio.h> #define a 10 main() { #define a 50 printf("%d",a); }

2 Answers  






main() { int i =0;j=0; if(i && j++) printf("%d..%d",i++,j); printf("%d..%d,i,j); }

1 Answers  


Given a list of numbers ( fixed list) Now given any other list, how can you efficiently find out if there is any element in the second list that is an element of the first list (fixed list)

3 Answers   Disney, Google, ZS Associates,


source code for delete data in array for c

1 Answers   TCS,


main( ) { void *vp; char ch = ‘g’, *cp = “goofy”; int j = 20; vp = &ch; printf(“%c”, *(char *)vp); vp = &j; printf(“%d”,*(int *)vp); vp = cp; printf(“%s”,(char *)vp + 3); }

1 Answers  


const int perplexed = 2; #define perplexed 3 main() { #ifdef perplexed #undef perplexed #define perplexed 4 #endif printf("%d",perplexed); } a. 0 b. 2 c. 4 d. none of the above

1 Answers   emc2, HCL,


main() { char name[10],s[12]; scanf(" \"%[^\"]\"",s); } How scanf will execute?

2 Answers  


Predict the Output: int main() { int *p=(int *)2000; scanf("%d",2000); printf("%d",*p); return 0; } if input is 20 ,what will be print

2 Answers  


Categories