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 |
main() { extern out; printf("%d", out); } int out=100;
How do you verify if the two sentences/phrases input is an anagram using predefined functions in string.h and by using arrays?
1) int i=5; j=i++ + i++ + i++; printf("%d",j);This code gives the answer 15.But if we replace the value of the j then anser is different?why? 2)int i=5; printf("%d",i++ + i++ + i++); this givs 18.
what is oop?
#include<stdio.h> main() { FILE *ptr; char i; ptr=fopen("zzz.c","r"); while((i=fgetch(ptr))!=EOF) printf("%c",i); }
what is variable length argument list?
void main() { int const * p=5; printf("%d",++(*p)); }
3 Answers Infosys, Made Easy, State Bank Of India SBI,
int a = 10 + 10 .... ,... A = A * A What would be the value of A? The answer is 120!! Could anyone explain this to me.
2 Answers Bosch, eInfochips, HCL, IHCL,
Write a routine to implement the polymarker function
create a C-code that will display the total fare of a passenger of a taxi if the driver press enter,the timer will stop. Every 10 counts is 2 pesos. Initial value is 25.00
void main() { static int i=i++, j=j++, k=k++; printf(“i = %d j = %d k = %d”, i, j, k); }
Develop a routine to reflect an object about an arbitrarily selected plane