Find if a number is power of two or not?

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

An arrangement of information in memory in such a way that it can be easily accessed and processed by a programming language a) string b) data structure c) pointers d) array

677


What is the mean of function?

640


What is the size of structure pointer in c?

603


What is a global variable in c?

581


What is the difference between a string and an array?

699






What are external variables in c?

535


Explain what are linked list?

612


swap 2 numbers without using third variable?

652


What is scope and lifetime of a variable in c?

564


Write a c program to demonstrate character and string constants?

1674


Can you please explain the scope of static variables?

590


What is static memory allocation? Explain

623


WHAT IS THE DEFINATION OF IN TECHNOLOGY AND OFF TECHNOLOGY ?

1850


Explain can you assign a different address to an array tag?

632


What is a structure member in c?

536