Give a oneline C expression to test whether a number is a
power of 2?
Answers were Sorted based on User's Feedback
Answer / dan
if (x & (x-1)) // false only if x is a power of 2
Is This Answer Correct ? | 102 Yes | 8 No |
Answer / ashutosh
I think
if (x & (x-1)) wont work when number is negative.
Is This Answer Correct ? | 12 Yes | 10 No |
Answer / fjords
if (int(log(x)) == log(x)) // log is to the base 2
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / rahul priyadarshi
#define ISPOW2(x) ((x==1)?1:(x&(x-1)))?0:1
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / yevgen
if ((num XOR (num - 1)) == num + num - 1) return true;
else return false;
Is This Answer Correct ? | 1 Yes | 0 No |
Write a program using one dimensional array to assign values and then display it on the screen. Use the formula a[i]=i*10 to assign value to an element.
1 Answers Samar State University,
How we print the table of 3 using for loop in c programing?
What is your nationality?
How to read a directory in a C program?
void func1(int (*a)[10]) { printf("Ok it works"); } void func2(int a[][10]) { printf("Will this work?"); } main() { int a[10][10]; func1(a); func2(a); } a. Ok it works b. Will this work? c. Ok it worksWill this work? d. None of the above
main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); }
There were 10 records stored in “somefile.dat” but the following program printed 11 names. What went wrong? void main() { struct student { char name[30], rollno[6]; }stud; FILE *fp = fopen(“somefile.dat”,”r”); while(!feof(fp)) { fread(&stud, sizeof(stud), 1 , fp); puts(stud.name); } }
Who could write how to find a prime number in dynamic array?
void main() { while(1){ if(printf("%d",printf("%d"))) break; else continue; } }
main() { int i = 258; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }
Write a program to implement the motion of a bouncing ball using a downward gravitational force and a ground-plane friction force. Initially the ball is to be projected in to space with a given velocity vector
void main() { if(~0 == (unsigned int)-1) printf(“You can answer this if you know how values are represented in memory”); }