Find if a number is power of two or not?



Find if a number is power of two or not?..

Answer / om

int f(int num)
{

if(( num>0) && (!(num & (num-1))) )
return 1;
else
return 0;
}
// f(16)...... 16 & 15 = 10000 & 01111 =00000
f(4) ...... 4 & 3 =0100 & 0111 =0000

f(11).......11 & 10 = 1011 & 1010 = 1010 so not...
f(12).......12 & 11 = 1100 & 1011 = 1000 so not...

Is This Answer Correct ?    6 Yes 0 No

Post New Answer

More C Interview Questions

What are the three constants used in c?

1 Answers  


What does the message "automatic aggregate intialization is an ansi feature" mean?

1 Answers  


What's wrong with "char *p; *p = malloc(10);"?

5 Answers  


Is main is a keyword in c?

1 Answers  


Can two or more operators such as and be combined in a single line of program code?

1 Answers  


Linked list is a Linear or non linear explain if linear how it working as a non linear data structures

1 Answers  


Find string palindrome 10marks

5 Answers   Honeywell, Infosys, Riktam, Roland,


What are void pointers in c?

1 Answers  


what is the different between if-else and switch statment (other than syntax)

26 Answers   CTS, Oracle, Scorpos,


wat are the two methods for swapping two numbers without using temp variable??

2 Answers  


WAP to accept basic salary of an employee? Calculate it HRA=25%,DA=30%,PF=30%&net salary display all contents?

6 Answers   Finite Infotech, Lovely Professional University, Wipro,


Tell me what are bitwise shift operators?

1 Answers  


Categories