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 ?    6 Yes 2 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 ?    21 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 ?    1 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 ?    3 Yes 1 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 ?    2 Yes 1 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 ?    12 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 ?    3 Yes 27 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 2 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 ?    1 Yes 0 No
Santhi Perumal
 
  Re: Function to find the given number is a power of 2 or not?
Answer
# 10
unsigned int is_power_of_2(unsigned int x)
{
return (x != 0) && ((x & (x - 1)) == 0);
}
 
Is This Answer Correct ?    5 Yes 0 No
Abhishek Sharma
 
  Re: Function to find the given number is a power of 2 or not?
Answer
# 11
#include<stdio.h>
int main()
{
int num,i=1;
printf("Enter the number:");
scanf("%d", &num);
do
{
i=i*2;
if(i==num)
{
printf("\n The no.is power of 2.");
break;
}
else if(i>num)
{
printf("\n The no. is not power of 2");
break;
}
}
while(i != num);
return 0;
}
 
Is This Answer Correct ?    2 Yes 0 No
Swathi Sharma
 
  Re: Function to find the given number is a power of 2 or not?
Answer
# 12
/* THIS SOLUTION IS 100% CORRECT */

#include<stdio.h>
int isPowerOf2(float n)
{
  while(n>1)
  {
  n/=2.0;
  }
  return (n==1)?1:0; /* 1-> TRUE; 0-> FALSE*/
}
void main()
{
  int x,n;
  printf("enter a number\n");
  fflush(stdin);
  scanf("%d",&n);
  x=isPowerOf2(n);
  if(x==1)
  printf("\n yes");
  else
  printf("\n no");
}
 
Is This Answer Correct ?    3 Yes 0 No
Rajiv Malhotra
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
can i know the source code for reversing a linked list with out using a temporary variable? Honeywell6
What is meant by global static? why we have to use static variable instead of Global variable L&T2
What is the memory allocated by the following definition ? int (*x)(); ADITI2
To find whether a number is even or odd without using any conditional operator?? IBM4
what is the other ways to find a logic to print whether a number is an even or odd wit out using % symbol??????? i know three different ways to print it. so i need any other different logic>>>>> TCS3
What is an object?  3
I need to take a sentence from input and sort the words alphabetically using the C programming language. Note: This is C not C++. qsort and strtok not allowed  4
How to swap two values using a single variable ? condition: Not to use Array and Pointer ?  4
what is compiler  5
what is call by value and call by reference  2
How can I set an array's size at run time?  7
What is the Difference between Macro and ordinary definition? Motorola2
value = 0xabcd; for (loop = 1; (value >> 1) & 1 | loop & 1; loop++) { foo(); if (loop & 1) value >>= 1; } how many times is foo() executed? Google5
related to rdbms query .  1
#include<stdio.h> main() { char s1[]="Ramco"; char s2[]="Systems"; s1=s2; printf("%s",s1); } Find the output CitiGroup4
wap in c to accept a number display the total count of digit  4
what is the use of pointers  5
how can i get this by using for loop? * ** * **** * ****** Excel3
main() { printf(5+"good morning"); printf("%c","abcdefgh"[4]); }the o/p is morning and e...how someone explain  1
the factorial of non-negative integer n is written n! and is defined as follows: n!=n*(n-1)*(n-2)........1(for values of n greater than or equal to 1 and n!=1(for n=0) Perform the following 1.write a c program that reads a non-negative integer and computes and prints its factorial. 2. write a C program that estimates the value of the mathematical constant e by using the formula: e=1+1/!+1/2!+1/3!+.... 3. write a c program the computes the value ex by using the formula ex=1+x/1!+xsquare/2!+xcube/3!+.... Ignou2
 
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