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 |
main() { char *a = "Hello "; char *b = "World"; clrscr(); printf("%s", strcpy(a,b)); } a. “Hello” b. “Hello World” c. “HelloWorld” d. None of the above
4 Answers Corporate Society, HCL,
why array index always strats wuth zero?
You are given any character string. Find the number of sets of vowels that come in the order of aeiou in the given string. For eg., let the given string be DIPLOMATIC. The answer returned must be "The number of sets is 2" and "The sets are "IO and AI". Vowels that form a singleton set must be neglected. Try to post the program executable in gcc or g++ or in java.
write a c-program to display the time using FOR loop
What is wrong with the following code? int *foo() { int *s = malloc(sizeof(int)100); assert(s != NULL); return s; }
Is it possible to type a name in command line without ant quotes?
main() { char name[10],s[12]; scanf(" \"%[^\"]\"",s); } How scanf will execute?
write a c-program to find gcd using recursive functions
main() { char *p; p="%d\n"; p++; p++; printf(p-2,300); }
#include<stdio.h> main() { const int i=4; float j; j = ++i; printf("%d %f", i,++j); }
how to swap 3 nos without using temporary variable
main() { char s[ ]="man"; int i; for(i=0;s[ i ];i++) printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]); }