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                      
Do you have a collection of Interview Questions and interested to share with us!!
Please send that collection to along with your userid / name. ThanQ
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C
 
 


 

 
 C interview questions  C Interview Questions
 C++ interview questions  C++ Interview Questions
 VC++ interview questions  VC++ Interview Questions
 Delphi interview questions  Delphi Interview Questions
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions
Question
Function to find the given number is a power of 2 or not?
 Question Submitted By :: Kantharaju
I also faced this Question!!     Rank Answer Posted By  
 
  Re: Function to find the given number is a power of 2 or not?
Answer
# 1
To be a power of 2 number,is to have a single 1 bit, and the
rest bits are zeros, lik2 1, 2, 4 , 8, 16, 32, 64, 128, ...

the bitsize of the number is sizeof(number) * 8

isPowerOf2() returns 1 if successful, or 0 (false) otherwise
int isPowerOf2 (number)
{
   int foundOnes = 0;
   int bitsize = sizeof(number) * 8;

   for (i = 0; i < bitsize; i++)
   {
      if (number & 1)
      {
        if(++foundOnes > 1)
           return false;
        /* shift the number to the right */
        number >> 1;        
      }
   }
   return foundOnes;
}
 
Is This Answer Correct ?    1 Yes 1 No
Hassan Noureddine
 
  Re: Function to find the given number is a power of 2 or not?
Answer
# 2
int ispow2(int number)
{
 if(n<o) {
 return 0;
 }

 else {
  
 return !(number&(number-1));

 }
 
Is This Answer Correct ?    6 Yes 1 No
Ms
 
 
 
  Re: Function to find the given number is a power of 2 or not?
Answer
# 3
int isPowrOf2 (unsigned int number)
{
   float x;

   if (number <= 0) return (0);

   x = log(number) / log(2);
   return ( (int) ( ((int)x*log(2))/log(number)));
}
 
Is This Answer Correct ?    0 Yes 0 No
Hassan Noureddine
 
  Re: Function to find the given number is a power of 2 or not?
Answer
# 4
int isPowerOf2(unsigned int n)
{
  float r=n;
  while(r>1) r/=2.0;
   return (r==1)?1:0; /* 1-> TRUE; 0-> FALSE*/
}
 
Is This Answer Correct ?    0 Yes 0 No
Jessu Srikanth
 
  Re: Function to find the given number is a power of 2 or not?
Answer
# 5
int ispwrof2(int num)
{
	int temp = 0;
	if(num <= 0)
		return 0;
	else if(num > 0)
	{
		while(num%2 == 0)
		{
			temp = num/2;
			num = temp;

		}

	 }
	 if(num == 1)
		return 1;
	 else
		return 0;
}
 
Is This Answer Correct ?    0 Yes 0 No
Sahil
 
  Re: Function to find the given number is a power of 2 or not?
Answer
# 6
Answer 2  Ms is correct and more efficient.

because if a number is power 2 it is in form 

1000---->8
100----->4
10------>2  like form
subtract one from that and do with bitwise and it should be
zero.

ie 1000(8) & 0111(7)== 0
 
Is This Answer Correct ?    6 Yes 0 No
Sivaraj
 
  Re: Function to find the given number is a power of 2 or not?
Answer
# 7
#include<stdio.h>
main()
{
int i;
if(i%2=0)
printf("The given number is a power of 2");
else
printf("The given number is not a power of 2");
return 0;
}
 
Is This Answer Correct ?    1 Yes 10 No
Sheikh Rasel
 
  Re: Function to find the given number is a power of 2 or not?
Answer
# 8
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
 int num,i=0,flag;
 clrscr();
 printf("\nEnter the number:=");
 scanf("%d",&num);
 
 if(num==0 || num==1 || (num%2)!=0) /* Validation Part */
 {
    printf("\n We can't express the given number to power 
of 2");
    return;
  }

 for(;;}
 {
   if(pow(2,i)==num)
   {
     flag=1;
     break;
  }
   if(pow(2,i)>num)
   {
     flag=0;
     break;
   }
i++;
}
if(falg==1)
 printf("\n %d number is 2 power of %d",num,i);
else
 printf("\n%d number can't be expressed as power of 2",num);

getch();
}
 
Is This Answer Correct ?    0 Yes 0 No
S.v.prasad Reddy,lifetree Conv
 
  Re: Function to find the given number is a power of 2 or not?
Answer
# 9
#include<stdio.h>
#include<conio.h>

int main()
{
	int i,count = 0,number;

	printf("Enter the number\n");
	scanf("%d",&number);

	for(i=15;i>=0;i--)
	{
		if((1<<i) & number)
			count++;
	}

	if(count == 1)
		printf("\nThe Given Number is Power of 2\n");
	else
		printf("\nThe Given Number is Not Power of 2\n");

}
 
Is This Answer Correct ?    0 Yes 0 No
Santhi Perumal
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
write a function to find whether a string is palindrome or not and how many palindrome this string contain?  1
Find Error if any in below code, Justify ur answer: struct xx { int a; struct yy { char c; struct xx* p; } struct yy* q; } NDS2
disadvantages of realloc ? HCL1
34.what are bitwise shift operators? 35.what are bit fields? What is the use of bit fields in a structure declaration? 36.what is the size of an integer variable? 37.what are the files which are automatically opened when a c file is executed? 38.what is the little endian and big endian? 39.what is the use of fflush() function? 40.what is the difference between exit() and _exit() functions? 41.where does malloc() function get the memory? 42.what is the difference between malloc() and calloc() function? 43.what is the difference between postfix and prefix unary increment operators?  2
Study the code: void show() main() { show(); } void show (char *s) { printf("%sn",s); } What will happen if it is compiled & run on an ANSI C Compiler? A)It will compile & nothing will be printed when it is executed B)it will compile but not link C)the compiler will generate an error D)the compiler will generate a warning Accenture3
what is diff between localstatic and globalstatis variable possible 2 use in another file...? HCL2
Write a function that accepts two numbers,say a and b and makes bth bit of a to 0.No other bits of a should get changed. Scientific-Atlanta2
sqrt(x+sqrt(x+sqrt(x+sqrt(x))))=2; Find the value of x? Subex1
how to convert binary to decimal and decimal to binary in C lanaguage  2
What is an object?  2
WAP to accept rollno,course name & marks of a student & display grade if total marks is above 200?  2
What is the real difference between arrays and pointers?  5
explain about storage of union elements. Bosch1
why i join syntel? Syntel11
post new interiew question and aptitude test papers  1
What does extern mean in a function declaration?  2
44.what is the difference between strcpy() and memcpy() function? 45.what is output of the following statetment? 46.Printf(“%x”, -1<<4); ? 47.will the program compile? int i; scanf(“%d”,i); printf(“%d”,i); 48.write a string copy function routine? 49.swap two integer variables without using a third temporary variable? 50.how do you redirect stdout value from a program to a file? 51.write a program that finds the factorial of a number using recursion?  3
Main must be written as a.the first function in the program b.Second function in the program c.Last function in the program d.any where in the program TCS10
1.what are local and global variables? 2.what is the scope of static variables? 3.what is the difference between static and global variables? 4.what are volatile variables? 5.what is the use of 'auto' keyword? 6.how do we make a global variable accessible across files? Explain the extern keyword? 7.what is a function prototype? 8.what does keyword 'extern' mean in a function declaration?  1
void main() { int i=5; printf("%d",i++ + ++i); } ME6
 
For more C 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