CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
How to write a C program to find the power of 2 in a normal way and in single step?


Category C Interview Questions
Rating (3.0) By 4 users
Added on 7/17/2006
Views 1594
Rate it!
Answers:

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.



assume : x is input number and want to check whethe x is power of two or not.
code: result = x & (x-1);
if( result == 1)
x is not power of two
else
x is power of two.



 Posted by: suresh kulal    

Contact suresh kulal  Contact suresh kulal

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.



assume : x is input number and want to check whethe x is power of two or not.
code: result = x & (x-1);
if( result == 1)
x is not power of two
else
x is power of two.



 Posted by: wwwwwww    

Contact wwwwwww  Contact wwwwwww

small modification in the above program.
{
result=x&(x-1);
if(x==0)
printf("number is power of 2");
else
prinf("its not power of 2");
}



 Posted by: santhosh    

Contact santhosh  Contact santhosh


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
I want C program code for : Reverse the links of a linked list by traversing only once

Input:
The input consists of the information in each node of the linked list.

Output:
The program displays the information in the linked list after the links are reversed.

Sample Input:
Enter the information in the linked list (Enter -1 to exit): 10
20
30
40
50
-1


Sample Output:
After the links are reversed
Information in the linked list:
50
40
30
20
10




View Answer
Write a program which accepts a filename as a command line argument and coverts all characters in the file to uppercase.



View Answer
Write a program which accepts a filename as a command line argument and reverses the contents of the file( ie first character

becomes the last character of the file and so on)


Input: The program takes the file name whose content should be reversed.

Output: The program reverses the contents of the file.



View Answer
How to type a string without using printf function?

View Answer
Whats is structure padding?Say a given structure
Struct{
int a;
char c;
float d;
}
the size of structure is 7 here.
But structure padding is done what will be the size of the struct?Will it change and how?How to avoid this?is it necessary?

View Answer
When is a switch statement better than multiple if statements?
View Answer
What is the difference between goto and longjmp() and setjmp()?
View Answer
What is an lvalue?
View Answer
Array is an lvalue or not?
View Answer
What is page thrashing?
View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/5944/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.64