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       Ask Questions on ANYTHING, that arise in your Daily Life at     FORUM9.COM
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 20 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 19 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 ?    5 Yes 13 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 27 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 ?    23 Yes 5 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 7 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 3 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 ?    4 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
 
What is the hidden bug with the following statement? assert(val++ != 0);  1
#include<stdio.h> main() { const int i=4; float j; j = ++i; printf("%d %f", i,++j); }  1
main() { extern int i; i=20; printf("%d",i); }  1
Extend the sutherland-hodgman clipping algorithm to clip three-dimensional planes against a regular paralleiepiped IBM1
main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); }  1
What is "far" and "near" pointers in "c"...?  3
main() { int i = 0xff ; printf("\n%d", i<<2); } a. 4 b. 512 c. 1020 d. 1024 HCL1
Write a function to find the depth of a binary tree. Adobe8
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
print numbers till we want without using loops or condition statements like specifically(for,do while, while swiches, if etc)!  7
#define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr, int)); }  1
main() { int x=5; clrscr(); for(;x<= 0;x--) { printf("x=%d ", x--); } } a. 5, 3, 1 b. 5, 2, 1, c. 5, 3, 1, -1, 3 d. –3, -1, 1, 3, 5 HCL1
main() { int i=10; i=!i>14; Printf ("i=%d",i); }  1
Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it. Wipro2
Program to find the largest sum of contiguous integers in the array. O(n)  7
main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); }  1
How will you print % character? a. printf(“\%”) b. printf(“\\%”) c. printf(“%%”) d. printf(“\%%”) HCL1
Given an array of size N in which every number is between 1 and N, determine if there are any duplicates in it. You are allowed to destroy the array if you like. Microsoft15
#include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; }  1
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 HCL1
 
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