Give a oneline C expression to test whether a number is a
power of 2?
Answers were Sorted based on User's Feedback
Answer / nmk
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
bool isPowerOf2(int number){
if ( number > 0 )
return (number & (number-1)) == 0;
if (number == 0)
return false;
if (isPowerOf2(-number)){
int count=0;
while ( !(number & 1)) {
++count;
number >>= 1;
}
if (fmod(count,2))
return true;
else
return false;
}
return false;
}
int main(void) {
for(int i=-1027; i<1025; ++i){
if (isPowerOf2(i) )
printf("%d\n", i);
}
return EXIT_SUCCESS;
}
| Is This Answer Correct ? | 2 Yes | 14 No |
Answer / carl menezes
#define ISPOW2(x) ( (x + (x-1) ) == (x<<1 + 1) )
| Is This Answer Correct ? | 10 Yes | 42 No |
Answer / sandhya
#include<stdio.h>
void main()
{
int number;
printf("|n enter a number");
scanf("%d",&number);
int twopower(int);
printf("\n the given number is ");
}
int twopower(int num)
{
while((num & -num)==num)
return(1);
printf("|n yes power of two");
}
| Is This Answer Correct ? | 9 Yes | 45 No |
Answer / mridul
#include<stdio.h>
main()
{
int a;
scanf("%d",&a);
if (a&1)
printf ("POWER off 2");
printf ("NOT power of 2");
}
| Is This Answer Correct ? | 3 Yes | 45 No |
How to reverse a String without using C functions ?
33 Answers Matrix, TCS, Wipro,
Write a program that find and print how many odd numbers in a binary tree
Print an integer using only putchar. Try doing it without using extra storage.
#include<stdio.h> int main() { int a=3,post,pre; post= a++ * a++ * a++; a=3; pre= ++a * ++a * ++a; printf("post=%d pre=%d",post,pre); return 0; }
main() { clrscr(); } clrscr();
Program to find the largest sum of contiguous integers in the array. O(n)
#include<conio.h> main() { int x,y=2,z,a; if(x=y%2) z=2; a=2; printf("%d %d ",z,x); }
main() { char not; not=!2; printf("%d",not); }
how to swap 3 nos without using temporary variable
const int perplexed = 2; #define perplexed 3 main() { #ifdef perplexed #undef perplexed #define perplexed 4 #endif printf("%d",perplexed); } a. 0 b. 2 c. 4 d. none of the above
main() { int a[10]; printf("%d",*a+1-*a+3); }
main() { int i=400,j=300; printf("%d..%d"); }