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

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

1 Answers  


Print an integer using only putchar. Try doing it without using extra storage.

2 Answers  


#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; }

3 Answers  


main() { clrscr(); } clrscr();

2 Answers  


Program to find the largest sum of contiguous integers in the array. O(n)

11 Answers  


#include<conio.h> main() { int x,y=2,z,a; if(x=y%2) z=2; a=2; printf("%d %d ",z,x); }

1 Answers  


main() { char not; not=!2; printf("%d",not); }

1 Answers  


how to swap 3 nos without using temporary variable

4 Answers   Satyam,


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

1 Answers   emc2, HCL,


main() { int a[10]; printf("%d",*a+1-*a+3); }

1 Answers  


main() { int i=400,j=300; printf("%d..%d"); }

3 Answers  


Categories