Give a one-line C expression to test whether a number is a
power of 2.
Answers were Sorted based on User's Feedback
Answer / lakshmi
void main()
{
int a;
scanf("%d",&a);
if((a&a-1)==0)
printf("Is a power of 2");
else
printf("Not a power of 2");
}
| Is This Answer Correct ? | 36 Yes | 8 No |
Answer / fallen angel
if(x&(x-1)==0)
then TRUE;
else
FALSE;
use the bitwise and operator
| Is This Answer Correct ? | 24 Yes | 2 No |
Answer / mayur shankariya
#include<stdio.h>
main()
{
int num;
printf("Enter Number
");
scanf("%d",&num);
(num & num - 1)? printf("Not
"):printf("Power of two
");
}
| Is This Answer Correct ? | 2 Yes | 0 No |
Answer / rarach
if( (a == 2 ) || ((a/2) %2 == 0 ) && a !=1)
TRUE
else
false
| Is This Answer Correct ? | 1 Yes | 3 No |
Answer / rohit
if((log(n)/log(2))/floor(log(n)/log(2))==1)
then TRUE;
else FALSE;
| Is This Answer Correct ? | 1 Yes | 5 No |
Answer / ramesh b penchal
#include<stdio.h>
main()
{
int n,i,d,m;
printf("Enter a number");
scanf("%d",&n);
m=n;
while(n>0)
{
d=n%10;
if(d!=0)
{
prntf("%d is not power of 2",m);
getch();
exit();
}
n=n/10;
}
prntf("%d is power of 2",m);
getch();
}
| Is This Answer Correct ? | 0 Yes | 5 No |
Answer / santhoo035
#include<iostream.h>
int main()
{
int n,i;
cout<<"Enter a number";
cin>>n;
for(i=1;i<n/2;i++)
{
if((2<<i)==n)
{
cout<<"The given no is power of 2";
break;
}
}
}
| Is This Answer Correct ? | 3 Yes | 9 No |
Answer / akshay rastogi
#include<conio.h>
void main()
{
int n,i=0,num=0;
printf("\n enter any number");
scanf("%d",&n);
while(num<=n)
{
i++;
num=2^i;
}
if(num==n)
printf("yes no. is power of 2");
else
printf("no.");
}
| Is This Answer Correct ? | 0 Yes | 6 No |
Answer / abc def
/*following expr evaluates to true if num is a power of
2.Else it's false. '&' - bitwise and.*/
(num == 1) || !(num & 1)
| Is This Answer Correct ? | 0 Yes | 9 No |
Answer / trailokya ranjan jena
#include<stdio.h>
void main()
{
int i,j;
clrscr();
printf("\n Enter a num");
scanf("%d",&i);
j=i;
for(;i%2==0;i/=2);
if(i==1)
printf("\n%d is power of 2",j);
else
printf("\n%d is not a power of 2");
getch();
}
| Is This Answer Correct ? | 4 Yes | 18 No |
main() { int i = 257; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }
to remove the repeated cahracter from the given caracter array. i.e.., if the input is SSAD output should of SAD
main() { char *str1="abcd"; char str2[]="abcd"; printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd")); }
What is wrong with the following code? int *foo() { int *s = malloc(sizeof(int)100); assert(s != NULL); return s; }
Set up procedure for generating a wire frame display of a polyhedron with the hidden edges of the object drawn with dashed lines
main() { int c = 5; printf("%d", main||c); } a. 1 b. 5 c. 0 d. none of the above
main(){ unsigned int i; for(i=1;i>-2;i--) printf("c aptitude"); }
Write a Program that Inputs 10 Numbers in an Array and Show the Maximum Number
There are 21 people in a room. They have to form groups of 3 people each. How many combinations are possible? Write a C program to print the same.
write a program to count the number the same (letter/character foreg: 's') in a given sentence.
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..
given integer number,write a program that displays the number as follows: First line :all digits second line : all except the first digit . . . . Last line : the last digit