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

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,


I need your help, i need a Turbo C code for this problem.. hope u'll help me guys.? Your program will have a 3x3 array. The user will input the sum of each row and each column. Then the user will input 3 values and store them anywhere, or any location or index, temporarily in the array. Your program will supply the remaining six (6) values and determine the exact location of each value in the array. Example: Input: Sum of row 1: 6 Sum of row 2: 15 Sum of row 3: 24 Sum of column 1: 12 Sum of column 2: 15 Sum of column 3: 18 Value 1: 3 Value 2: 5 Value 3: 6 Output: Sum of Row 1 2 3 6 4 5 6 15 7 8 9 24 Sum of Column 12 15 18 Note: Your program will not necessary sort the walues in the array Thanks..

0 Answers  


what will be the output of this program? void main() { int a[]={5,10,15}; int i=0,num; num=a[++i] + ++i +(++i); printf("%d",num); }

3 Answers   Wipro,


main() { static char names[5][20]={"pascal","ada","cobol","fortran","perl"}; int i; char *t; t=names[3]; names[3]=names[4]; names[4]=t; for (i=0;i<=4;i++) printf("%s",names[i]); }

2 Answers  


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

9 Answers   CSC, GoDB Tech, IBM,






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

1 Answers  


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

1 Answers  


Write a program using one dimensional array to assign values and then display it on the screen. Use the formula a[i]=i*10 to assign value to an element.

1 Answers   Samar State University,


int aaa() {printf(“Hi”);} int bbb(){printf(“hello”);} iny ccc(){printf(“bye”);} main() { int ( * ptr[3]) (); ptr[0] = aaa; ptr[1] = bbb; ptr[2] =ccc; ptr[2](); }

1 Answers  


#include<stdio.h> main() { FILE *ptr; char i; ptr=fopen("zzz.c","r"); while((i=fgetch(ptr))!=EOF) printf("%c",i); }

1 Answers  


write a program in c to merge two array

2 Answers  


In the following pgm add a stmt in the function fun such that the address of 'a' gets stored in 'j'. main(){ int * j; void fun(int **); fun(&j); } void fun(int **k) { int a =0; /* add a stmt here*/ }

1 Answers  


Categories