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 |
struct aaa{ struct aaa *prev; int i; struct aaa *next; }; main() { struct aaa abc,def,ghi,jkl; int x=100; abc.i=0;abc.prev=&jkl; abc.next=&def; def.i=1;def.prev=&abc;def.next=&ghi; ghi.i=2;ghi.prev=&def; ghi.next=&jkl; jkl.i=3;jkl.prev=&ghi;jkl.next=&abc; x=abc.next->next->prev->next->i; printf("%d",x); }
‎#define good bad main() { int good=1; int bad=0; printf ("good is:%d",good); }
main() { int i = 0xff ; printf("\n%d", i<<2); } a. 4 b. 512 c. 1020 d. 1024
main() { char c=' ',x,convert(z); getc(c); if((c>='a') && (c<='z')) x=convert(c); printf("%c",x); } convert(z) { return z-32; }
void main() { void *v; int integer=2; int *i=&integer; v=i; printf("%d",(int*)*v); }
main() { int a[10]; printf("%d",*a+1-*a+3); }
#include<stdio.h> main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q; p=&a[2][2][2]; *q=***a; printf("%d----%d",*p,*q); }
Write a program to model an exploding firecracker in the xy plane using a particle system
what is brs test reply me email me kashifabbas514@gmail.com
Given a list of numbers ( fixed list) Now given any other list, how can you efficiently find out if there is any element in the second list that is an element of the first list (fixed list)
3 Answers Disney, Google, ZS Associates,
Write a program that find and print how many odd numbers in a binary tree
Write a single line c expression to delete a,b,c from aabbcc