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 |
void main() { int k=ret(sizeof(float)); printf("\n here value is %d",++k); } int ret(int ret) { ret += 2.5; return(ret); }
pls anyone can help me to write a code to print the values in words for any value.Example:1034 to print as "one thousand and thirty four only"
Given n nodes. Find the number of different structural binary trees that can be formed using the nodes.
16 Answers Aricent, Cisco, Directi, Qualcomm,
Give a very good method to count the number of ones in a 32 bit number. (caution: looping through testing each bit is not a solution)
void main() { if(~0 == (unsigned int)-1) printf(“You can answer this if you know how values are represented in memory”); }
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
What is "far" and "near" pointers in "c"...?
main() { char p[ ]="%d\n"; p[1] = 'c'; printf(p,65); }
main() { int i; float *pf; pf = (float *)&i; *pf = 100.00; printf("\n %d", i); } a. Runtime error. b. 100 c. Some Integer not 100 d. None of the above
Question: We would like to design and implement a programming solution to the reader-writer problem using semaphores in C language under UNIX. We assume that we have three readers and two writers processes that would run concurrently. A writer is to update (write) into one memory location (let’s say a variable of type integer named temp initialized to 0). In the other hand, a reader is to read the content of temp and display its content on the screen in a formatted output. One writer can access the shared data exclusively without the presence of other writer or any reader, whereas, a reader may access the shared memory for reading with the presence of other readers (but not writers).
main() { int x=5; clrscr(); for(;x<= 0;x--) { printf("x=%d ", x--); } } a. 5, 3, 1 b. 5, 2, 1, c. 5, 3, 1, -1, 3 d. –3, -1, 1, 3, 5
main() { int i =0;j=0; if(i && j++) printf("%d..%d",i++,j); printf("%d..%d,i,j); }