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 |
main() { int i=5; printf("%d",++i++); }
main() { int i =10, j = 20; clrscr(); printf("%d, %d, ", j-- , --i); printf("%d, %d ", j++ , ++i); } a. 20, 10, 20, 10 b. 20, 9, 20, 10 c. 20, 9, 19, 10 d. 19, 9, 20, 10
void main() { int i=5; printf("%d",i+++++i); }
find simple interest & compund interest
Implement a t9 mobile dictionary. (Give code with explanation )
1 Answers Amazon, Peak6, Yahoo,
Give a oneline C expression to test whether a number is a power of 2?
25 Answers EA Electronic Arts, Google, Motorola,
How we print the table of 3 using for loop in c programing?
main() { char name[10],s[12]; scanf(" \"%[^\"]\"",s); } How scanf will execute?
What is the output for the following program main() { int arr2D[3][3]; printf("%d\n", ((arr2D==* arr2D)&&(* arr2D == arr2D[0])) ); }
program to find the roots of a quadratic equation
14 Answers College School Exams Tests, Engineering, HP, IIIT, Infosys, Rajiv Gandhi University of Knowledge Technologies RGUKT, SSC,
main() { unsigned int i=65000; while(i++!=0); printf("%d",i); }
Who could write how to find a prime number in dynamic array?