ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   SiteMap shows list of All Categories in this site.
Google
 
Categories  >>  Code Snippets  >>  Programming Code  >>  C Code
 
 


 

 
 C Code interview questions  C Code Interview Questions
 C++ Code interview questions  C++ Code Interview Questions
 VC++ Code interview questions  VC++ Code Interview Questions
 Java Code interview questions  Java Code Interview Questions
 Dot Net Code interview questions  Dot Net Code Interview Questions
 Visual Basic Code interview questions  Visual Basic Code Interview Questions
 Programming Code AllOther interview questions  Programming Code AllOther Interview Questions
Question
Give a oneline C expression to test whether a number is a 
power of 2?
 Question Submitted By :: Solidcube
I also faced this Question!!     Rank Answer Posted By  
 
  Re: Give a oneline C expression to test whether a number is a power of 2?
Answer
# 1
#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 ?    1 Yes 18 No
Sandhya
 
  Re: Give a oneline C expression to test whether a number is a power of 2?
Answer
# 2
#define ISPOW2(x) ( (x + (x-1) ) == (x<<1 + 1) )
 
Is This Answer Correct ?    4 Yes 17 No
Carl Menezes
 
 
 
  Re: Give a oneline C expression to test whether a number is a power of 2?
Answer
# 3
main()
{
int a;
scanf("%d",&a);
if((a+(a-1))==((a<<1)-1))
printf("it is powers of 2");
else
printf("not powers of 2");
}
 
Is This Answer Correct ?    4 Yes 11 No
Sathish
 
  Re: Give a oneline C expression to test whether a number is a power of 2?
Answer
# 4
#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 ?    0 Yes 25 No
Mridul
 
  Re: Give a oneline C expression to test whether a number is a power of 2?
Answer
# 5
if (x & (x-1))  // false only if x is a power of 2
 
Is This Answer Correct ?    22 Yes 4 No
Dan
 
  Re: Give a oneline C expression to test whether a number is a power of 2?
Answer
# 6
#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 ?    1 Yes 6 No
Nmk
 
  Re: Give a oneline C expression to test whether a number is a power of 2?
Answer
# 7
#include<stdio.h>

void main()
{
int a,i;
scanf("%d",&a);

for( i = 0; a != 0; a = a >> 1)
	if( a & 0x01 )
		i++;
if( i == 1 )
printf ("POWER off 2");
else
printf (" Not power of 2");
}
 
Is This Answer Correct ?    1 Yes 4 No
Raghavendra Donnur
 
  Re: Give a oneline C expression to test whether a number is a power of 2?
Answer
# 8
# include <stdio.h>

void main()
{
       int n;
       scanf("%d",&n);
       if( n & (n-1) )
           printf("Not a Power of 2");
       else
           printf("Power of 2");
}
 
Is This Answer Correct ?    20 Yes 2 No
Vadivel
 
  Re: Give a oneline C expression to test whether a number is a power of 2?
Answer
# 9
I think
if (x & (x-1))   wont work when number is negative.
 
Is This Answer Correct ?    3 Yes 6 No
Ashutosh
 
  Re: Give a oneline C expression to test whether a number is a power of 2?
Answer
# 10
if (x && !(x & (x-1)) == 0)
 
Is This Answer Correct ?    1 Yes 1 No
Phil
 
  Re: Give a oneline C expression to test whether a number is a power of 2?
Answer
# 11
printf("%u=%u\n", x, ((x & ~(x - 1)) == x));
 
Is This Answer Correct ?    1 Yes 1 No
Natmat
 
  Re: Give a oneline C expression to test whether a number is a power of 2?
Answer
# 12
x & (x - 1) == x + (x - 1)
 
Is This Answer Correct ?    0 Yes 3 No
Nikos
 
  Re: Give a oneline C expression to test whether a number is a power of 2?
Answer
# 13
#include<stdio.h>
main()
{
   int n,c=0,i;
   for(i=0;i<(sizeof(n)*8);i++)
      if(n&(1<<i))
        c++;
   if(c==1)
     printf("%d is powwer of 2",n);
   else
     printf("%d is not a power of 2",n);
}
 
Is This Answer Correct ?    0 Yes 1 No
Valli
 
  Re: Give a oneline C expression to test whether a number is a power of 2?
Answer
# 14
n%2
 
Is This Answer Correct ?    1 Yes 4 No
Ataraxic
 
  Re: Give a oneline C expression to test whether a number is a power of 2?
Answer
# 15
Previous post  -- It's wrong... absolutely...
 
Is This Answer Correct ?    4 Yes 0 No
Ataraxic
 
  Re: Give a oneline C expression to test whether a number is a power of 2?
Answer
# 16
main()
{

        int a[30];
        int i=0;
        for(i=0; i<30; i++)
                a[i]=i;
        for(i=0; i<30;i++)
        {
                if(!(a[i] & a[i-1]))
                printf("%d is power of 2\n",a[i]);
                else
                printf("%d is not a power of 2\n",a[i]);
        }



}
 
Is This Answer Correct ?    0 Yes 1 No
Xyz
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
main() { int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++||l++; printf("%d %d %d %d %d",i,j,k,l,m); }  1
main(int argc, char *argv[]) { (main && argc) ? main(argc-1, NULL) : return 0; } a. Runtime error. b. Compile error. Illegal syntax c. Gets into Infinite loop d. None of the above HCL1
Derive expression for converting RGB color parameters to HSV values  1
main() { char *a = "Hello "; char *b = "World"; clrscr(); printf("%s", strcpy(a,b)); } a. “Hello” b. “Hello World” c. “HelloWorld” d. None of the above HCL1
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 HCL1
#define FALSE -1 #define TRUE 1 #define NULL 0 main() { if(NULL) puts("NULL"); else if(FALSE) puts("TRUE"); else puts("FALSE"); }  1
main() { char c; int i = 456; clrscr(); c = i; printf("%d", c); } a. 456 b. -456 c. random number d. none of the above HCL1
Extend the sutherland-hodgman clipping algorithm to clip three-dimensional planes against a regular paralleiepiped IBM1
main() { printf("%d", out); } int out=100;  1
What are the files which are automatically opened when a C file is executed?  1
int aaa() {printf(“Hi”);} int bbb(){printf(“hello”);} iny ccc(){printf(“bye”);} main() { int ( * ptr[3]) (); ptr[0] = aaa; ptr[1] = bbb; ptr[2] =ccc; ptr[2](); }  1
main(){ char a[100]; a[0]='a';a[1]]='b';a[2]='c';a[4]='d'; abc(a); } abc(char a[]){ a++; printf("%c",*a); a++; printf("%c",*a); }  1
void main() { static int i=i++, j=j++, k=k++; printf(“i = %d j = %d k = %d”, i, j, k); }  1
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); }  1
In the following pgm add a stmt in the function fun such that the address of 'a' gets stored in 'j'. main(){ int * j; void fun(int **); fun(&j); } void fun(int **k) { int a =0; /* add a stmt here*/ }  1
main() { register int a=2; printf("Address of a = %d",&a); printf("Value of a = %d",a); }  1
void main() { int *mptr, *cptr; mptr = (int*)malloc(sizeof(int)); printf(“%d”,*mptr); int *cptr = (int*)calloc(sizeof(int),1); printf(“%d”,*cptr); }  1
main() { float i=1.5; switch(i) { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } }  1
Find the largest number in a binary tree Infosys4
what will be the position of the file marker? a: fseek(ptr,0,SEEK_SET); b: fseek(ptr,0,SEEK_CUR);  1
 
For more C Code Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com