GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 224 of 438    Print  
How to write a C program to find the power of 2 in a normal way and in single step?

  
Total Answers and Comments: 9 Last Update: June 04, 2008     Asked by: anjali 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
July 09, 2006 07:27:48   #1  
t.lavanya        

RE: How to write a C program to find the power of 2 in...

if(x && x-1 ==0) printf(" no. is power of 2");


 
Is this answer useful? Yes | No
July 11, 2006 03:35:59   #2  
Suman        

RE: How to write a C program to find the power of 2 in...
U can take logarithm base 2, and check the result is in interger form or floating point form, u can check whether it is power of 2 or not.
 
Is this answer useful? Yes | No
July 30, 2006 08:31:15   #3  
farha        

RE: How to write a C program to find the power of 2 in...

Lavanya,

Can u explain logic. The If condition


 
Is this answer useful? Yes | No
August 01, 2006 00:29:30   #4  
Anurag Laddha        

RE: How to write a C program to find the power of 2 in...
That's actually logical ANDingThe actual to determine whether a number is a power of 2 or not is --> bit wise AND the number under consideration with its previous number. if the result is zero then the number under consideration is power of 2Code: if (x & (x-1) == 0) number is a power of 2else number is NOT a power of two.
 
Is this answer useful? Yes | No
August 04, 2006 01:01:19   #5  
newbie        

RE: How to write a C program to find the power of 2 in...
what will happen if x is 0?
 
Is this answer useful? Yes | No
August 20, 2006 12:37:24   #6  
Hinstings Member Since: August 2006   Contribution: 4    

RE: How to write a C program to find the power of 2 in...
You are right. We should first check whether the given number is larger than 0. If it is larger than 0, we can use (x && (x-1) == 0) to determine whether x is power of 2 or not. If it is 0, then return 0. If x is smaller than 0, what should we do?
 
Is this answer useful? Yes | No
August 20, 2006 12:40:14   #7  
Hinstings Member Since: August 2006   Contribution: 4    

RE: How to write a C program to find the power of 2 in...
My mistake, actually, what I want to say is "if the given number is like 2^(-2)"? Can we check that.
 
Is this answer useful? Yes | No
October 22, 2007 00:00:04   #8  
krisgroup Member Since: October 2007   Contribution: 8    

RE: How to write a C program to find the power of 2 in...

Hi folks,

I would say try this ...

int result=!((input_no&&input_no-1)&&input_no)

it works a good bit twidling technique,

chaitanya


 
Is this answer useful? Yes | No
June 04, 2008 13:50:57   #9  
aravindkumarsingh Member Since: June 2008   Contribution: 2    

RE: How to write a C program to find the power of 2 in a normal way and in single step?
# include <stdio.h>
# include <conio.h>
void main()
{
 int x;
 printf("Enter the value of x: ");
 scanf(" %d", &x);
 if((x & (x-1)) ==0)
  printf(" no. is power of 2");
    else
      printf(" no. is not power of 2");

 getch();
}

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape