Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


Give a oneline C expression to test whether a number is a
power of 2?

Answers were Sorted based on User's Feedback



Give a oneline C expression to test whether a number is a power of 2? ..

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

Give a oneline C expression to test whether a number is a power of 2? ..

Answer / ataraxic

n%2

Is This Answer Correct ?    1 Yes 25 No

Give a oneline C expression to test whether a number is a power of 2? ..

Answer / carl menezes

#define ISPOW2(x) ( (x + (x-1) ) == (x<<1 + 1) )

Is This Answer Correct ?    10 Yes 42 No

Give a oneline C expression to test whether a number is a power of 2? ..

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

Give a oneline C expression to test whether a number is a power of 2? ..

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

Post New Answer

More C Code Interview Questions

main( ) { void *vp; char ch = ‘g’, *cp = “goofy”; int j = 20; vp = &ch; printf(“%c”, *(char *)vp); vp = &j; printf(“%d”,*(int *)vp); vp = cp; printf(“%s”,(char *)vp + 3); }

1 Answers  


write a program in c language to get the value of arroy keys pressed and display the message which arrow key is pressed?

1 Answers  


could you please send the program code for multiplying sparse matrix in c????

0 Answers  


{ int *ptr=(int*)malloc(sizeof(int)); *ptr=4; printf("%d",(*ptr)+++*ptr++); }

4 Answers  


What is the output of the program given below main() { signed char i=0; for(;i>=0;i++) ; printf("%d\n",i); }

1 Answers  


How to reverse a String without using C functions ?

33 Answers   Matrix, TCS, Wipro,


Write a single line c expression to delete a,b,c from aabbcc

2 Answers   Microsoft,


main() { { unsigned int bit=256; printf("%d", bit); } { unsigned int bit=512; printf("%d", bit); } } a. 256, 256 b. 512, 512 c. 256, 512 d. Compile error

1 Answers   HCL,


struct Foo { char *pName; char *pAddress; }; main() { struct Foo *obj = malloc(sizeof(struct Foo)); clrscr(); obj->pName = malloc(100); obj->pAddress = malloc(100); strcpy(obj->pName,"Your Name"); strcpy(obj->pAddress, "Your Address"); free(obj); printf("%s", obj->pName); printf("%s", obj->pAddress); } a. Your Name, Your Address b. Your Address, Your Address c. Your Name Your Name d. None of the above

2 Answers   HCL,


main() { extern out; printf("%d", out); } int out=100;

1 Answers  


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

2 Answers  


Write a complete program that consists of a function that can receive two numbers from a user (M and N) as a parameter. Then print all the numbers between the two numbers including the number itself. If the value of M is smaller than N, print the numbers in ascending flow. If the value of M is bigger than N, print the numbers in descending flow. may i know how the coding look like?

2 Answers  


Categories