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 | 1 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 |
Write a program to check whether the number is prime and also check if it there i n fibonacci series, then return true otherwise return false
x=2 y=3 z=2 x++ + y++; printf("%d%d" x,y);
write a program to Insert in a sorted list
what is variable length argument list?
what is the output of following program ? void main() { int i=5; printf("%d %d %d %d %d ",i++,i--,++i,--i,i); }
how to check whether a linked list is circular.
#define FALSE -1 #define TRUE 1 #define NULL 0 main() { if(NULL) puts("NULL"); else if(FALSE) puts("TRUE"); else puts("FALSE"); }
{ int *ptr=(int*)malloc(sizeof(int)); *ptr=4; printf("%d",(*ptr)+++*ptr++); }
What are segment and offset addresses?
write a c-program to find gcd using recursive functions
char inputString[100] = {0}; To get string input from the keyboard which one of the following is better? 1) gets(inputString) 2) fgets(inputString, sizeof(inputString), fp)
how to swap 3 nos without using temporary variable