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 is the difference between #include<stdio.h> and #include"stdio.h" ?

6 Answers   TCS,


explain memory layout of a C program

2 Answers  


Write a c program to print the sizes and ranges of different data types in c?

1 Answers  


what is the disadvantage of using macros?

1 Answers   Wipro,


When should the volatile modifier be used?

0 Answers  






How many types of sorting are there in c?

0 Answers  


What is #line in c?

0 Answers  


Why is C language being considered a middle level language?

0 Answers  


What is a c token and types of c tokens?

0 Answers  


what is c

4 Answers  


write c program without semicolon

11 Answers   MindTech, TCS, Wipro,


What does volatile do?

0 Answers  


Categories