Give a oneline C expression to test whether a number is a
power of 2?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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
why is printf("%d %d %d",i++,--i,i--);
main() { int i; i = abc(); printf("%d",i); } abc() { _AX = 1000; }
main() { int i = 257; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }
#include <stdio.h> #define a 10 main() { #define a 50 printf("%d",a); }
main() { int i =0;j=0; if(i && j++) printf("%d..%d",i++,j); printf("%d..%d,i,j); }
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
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); }
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
main() { char name[10],s[12]; scanf(" \"%[^\"]\"",s); } How scanf will execute?
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